Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top