3

So I have a base ItemTable, and then a number of Tables that inherit from it. I don't seem to be able to modify the Meta class. I tried just including the meta class normally and it didn't work, then I found this bug report and implemented it below. It fails silently: the tables render only with the columns from the parent meta class.

class ItemTable(tables.Table):

    class Meta:
        model = Item
        attrs = {"class":"paleblue"}
        fields = ('name', 'primary_tech', 'primary_biz', 'backup_tech', 'backup_biz')

class ApplicationTable(ItemTable):

    def __init__(self, *args, **kwargs):
        super(ApplicationTable, self).__init__(*args, **kwargs)

    class Meta(ItemTable.Meta):
        model = Application
        fields += ('jira_bucket_name',)

EDIT: Code amended as shown. I now get a NameError that fields is not defined.

thumbtackthief
  • 6,093
  • 10
  • 41
  • 87

3 Answers3

5

Try:

class ApplicationTable(ItemTable):
    class Meta:
        model = Application
        fields = ItemTable.Meta.fields + ('jira_bucket_name',)

You'll have the same problems extending Meta in a table, as you will in a normal Django model.

bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
  • This fails silently, but does not add the new columns. FWIW, this is the first time I've ever tried extending `Meta`, so I don't know how it compares to a non-django-tables2 model. – thumbtackthief Feb 14 '14 at 21:20
  • There was an embarrassing level of idiocy on my part that kept this from working. This is the correct solution. Thanks! – thumbtackthief Feb 18 '14 at 19:54
4

You didnt add , (comma) to one-element tuple. Try to change this line Meta.attrs['fields'] += ('jira_bucket_name') in ApplicationTable to:

Meta.attrs['fields'] += ('jira_bucket_name',)

if it didnt help try to create Meta class outsite model class definition:

class ItemTableMeta:
    model = Item
    attrs = {"class":"paleblue"}
    fields = ('name', 'primary_tech', 'primary_biz', 'backup_tech', 'backup_biz')

class ApplicationTableMeta(ItemTableMeta):
    model = Application
    fields = ItemTableMeta.fields + ('jira_bucket_name',)


class ItemTable(tables.Table):
    #...
    Meta = ItemTableMeta

class ApplicationTable(ItemTable):
    #...
    Meta = ApplicationTableMeta
ndpu
  • 22,225
  • 6
  • 54
  • 69
0

You may need to take this up with the django-tables author. This is not a problem with standard Django.

Paul Bissex
  • 1,611
  • 1
  • 17
  • 22