我在Django中使用SingleTableMixin
和FilterView
来呈现过滤器表单和表。在基本层面上,它运作得很好。在
现在,表的列数与模型的字段数一样多(正如它应该的那样),但是我只想呈现用户在filter表单中为其提供输入的列,并动态地排除其他列。在
我正试图通过使用def get_table_kwargs(self):
来实现这一点
这是我的代码:
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
}
我当前收到一个错误消息:“TypeError at/table/
“dict”对象不可调用”
那是因为我是个笨蛋。因此,我非常感谢您对这个由filtered = self.filterset.form.data()
引起的特定错误的评论,以及对一般逻辑的评论。在
先谢谢你!在
我自己找到了解决办法。因此,如果有其他人也将django-tables2与django filter结合起来,并希望进行动态列呈现,这取决于用户是否过滤了相应的字段。在
我的错误是试图访问filterset.form.data要获得用户输入,当我必须请求时:
相关问题 更多 >
编程相关推荐