django-tables2列集

时间:2012-07-24 11:38:13

标签: django django-tables2

如何告诉django-tables2我希望在表格中包含哪些列?我知道有Column attribute'可见',可以设置为False。 但是我有一个包含许多字段的模型,并且只想显示其中的一些,所以编写所有列的完整列表,只是为了告诉它们大部分都不可见,这似乎不是正确的方法。

我正在寻找的是一种提供要显示的列名列表的方法,如果可行的话甚至可以让用户选择他想要的列。

我想到了另一个解决方案 - 默认情况下将'visible'属性设为False,但由于它是在Column类中定义的,我仍然需要编写一个完整的列表。

由于我没有找到任何django-tables2讨论论坛,我在这里问。

1 个答案:

答案 0 :(得分:26)

指定模型字段的示例

您的模型

class Product(model.Models):
    name = model.CharField(max_length=20)
    price = model.DecimalField(max_digit=9, decimal_places=2)

您的表

class ProductTable(tables.Table):
    actions = ProductActions(orderable=False) # custom tables.Column()
    class Meta:
        model = Product
        fields = ('name', 'price', 'action') # fields to display

您也可以使用exclude

Related docs entry here