W moim projekcie Django potrzebuję tabel, które są kolumnami dynamicznymi i zależą od tego, co jest w bazie danych. Więc znalazłem rozwiązanie w here i działa, ale z małym problemem. Tutaj jest klasa z tabeli Mam rozciągającej się dynamicznie:Django-tables2 - dynamiczne dodawanie kolumn do tabeli - nie dodawanie attrs do tagu tabeli w html
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
I tu jest metoda, która rozszerza klasę:
def define_table(roles):
attrs = dict((r.name, tables.Column() for r in roles)
klass = type('DynamicTable', (ClientsTable,), attrs)
return klass
Kiedy tworzę tabelę w views.py tak:
table = define_table(roles)(queryset)
tabela przedstawia kolumny jak chciałem, ale w kodzie html widzę, że zignorował attrs:
{"class": "paleblue", "orderable":"True", "width":"100%"}
Nie ma więc stylu css dla paleblue, co jest dla mnie ważne. Czuję, że może to być coś z klasy Meta, ale pola i model działają, więc nie mam pojęcia, dlaczego attrs nie są.
ładne rzeczy! Niezupełnie odpowiadając na mój problem, ale mogłem zmodyfikować twój fragment kodu, aby rozwiązać mój problem i nauczyłem się dużo o Pythonie typu "typ" i meta klasy działania. Thx a bunch! – schwobaseggl
Cieszę się, że pomogłem! Napisałem też różne posty robiące rzeczy typu na moim blogu: http://spapas.github.io/category/django.html – Serafeim