In my Django project I need to have tables which columns are dynamic and depend on what is in the database. So I found a solution in here and it works but with a little problem. Here's the class with a table I'm extending dynamically:
class ClientsTable(tables.Table):
class Meta:
model = Client
attrs = {"class": "paleblue", "orderable":"True", "width":"100%"}
fields = ('name',)
def __init__(self, *args, **kwargs):
super(ClientsTable, self).__init__(*args, **kwargs)
self.counter = itertools.count()
def render_row_number(self):
return '%d' % next(self.counter)
def render_id(self, value):
return '%s' % value
And here is the method that extends the class:
def define_table(roles):
attrs = dict((r.name, tables.Column() for r in roles)
klass = type('DynamicTable', (ClientsTable,), attrs)
return klass
When I'm creating a table in views.py like this:
table = define_table(roles)(queryset)
The table shows columns like I wanted, but in the html code I see that it ignored the attrs:
{"class": "paleblue", "orderable":"True", "width":"100%"}
So there is no css style for paleblue, which is important to me. I feel that it might be something with Meta class but fields and model are working, so I have no idea why attrs are not.
type
and meta class action. Thx a bunch! – Fawn