Search code examples
pythondjangodjango-tables2

Django-tables2 - how to set default value for all columns?


Django-tables2 renders double dash in case there is no value in the cell. We can change it explicitely specifying default attribute for every column.

Can I do that for all columns at once?

Of course I tried to override __init__ method but Table.columns can't be modified and Table.base_columns seems to cause the same problem.

def __init__(self,*args,**kwargs):
    for name,col in self.base_columns.items():
        col.default = ''

    super(DopytyTable,self).__init__(*args,**kwargs)

raises

col.default = '' 
AttributeError: can't set attribute

Solution

  • I haven't figured out how to modify columns inside __init__ method but there is even more simple way to do that. Unfortunately it isn't documented but it works correctly for me.

    Just add default attribute to class Meta:

    class Table(Table):
        class Meta:
            default = ''