Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-tables2 set of columns

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.

like image 506
liepumartins Avatar asked Jul 24 '12 11:07

liepumartins


1 Answers

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

like image 103
Pierre de LESPINAY Avatar answered Sep 23 '22 06:09

Pierre de LESPINAY