How to tell django-tables2 which columns I would like to have in table? I know there is this Column attribute 'visible', which can be set to False. However I have a model with many fields, and would like to display just some of them, so writing a complete list of all columns, just to tell that most of them will not be visible, does not seem the right approach.

What I am looking for is a way to provide list of column names to be displayed, if this is possible then maybe even give user the ability to select which columns he wants.

The other solution came to my mind - make that 'visible' attribute False by default, but since it is defined in Column class, I would still need to write a complete list.

Since I have not found any django-tables2 discussion forum, I ask here.

有帮助吗?

解决方案

Example of specifying model fields

Your Model

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

Your Table

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

Also you can also use exclude

Related docs entry here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top