Search code examples
pythondjangodjango-tables2

Inherit and modify a `Meta` class


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.


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.