16

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.

liepumartins
  • 464
  • 2
  • 6
  • 21
  • Do you mean show/hide columns dynamically or just configure which fields to display at runtime ? – Pierre de LESPINAY Aug 10 '12 at 07:43
  • Primary goal is to determine which fields to display by providing a set of column names, dynamic part is optional. I don't actually see how the dynamic part could be achieved without my primary goal. – liepumartins Aug 13 '12 at 07:05
  • You should use `Meta: fields: ()` or `exclude: ()` in your `tables.Table` definition shouldn't you ? – Pierre de LESPINAY Aug 13 '12 at 09:40

1 Answers1

25

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

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307