I'm using SingleTableMixin
and FilterView
in Django to render a filter form and table. On the basic level it works very well.
Now, the table has as many columns as the model has fields (as it should), but I would like to render ONLY the columns for which a user has provided input in the filter form and exclude the others columns dynamically.
I'm trying to achieve this by using def get_table_kwargs(self):
Here is my code:
class FactListView(SingleTableMixin, FilterView):
table_class = FactTable
filterset_class = FactFilter
template_name = 'main/table.html'
def get_table_kwargs(self):
filtered = self.filterset.form.data()
columns = {}
fact_fields = [
"field_name1",
"field_name2",
"field_name3",
"other fields",
"...",
]
for field, value in filtered():
if value is not None:
columns.update({field: value})
fact_fields[:] = [x for x in fact_fields if x not in columns]
return {
'exclude': fact_fields
}
I'm currently getting an error saying: "TypeError at /table/
'dict' object is not callable"
That's because I'm a noob. So I would be very grateful for comments on this particular error, which results from filtered = self.filterset.form.data()
, as well as on the general logic.
Many thanks in advance!