Can't exclude fields - django tables 2

Hello everyone!

So, I want to render a table with django_tables2.

I can accomplish the render of the table correctly but I can’t modify which fields are included or excluded.

On the shell I can get what I want but I don’t know how to integrate that table into SingleTableView maintaining the exclusion.

In [1] class ClientTable(tables.Table):
    ...:     class Meta:
    ...:         model = Client
    ...:         fields = ("name", )

In [2]: ClientTable.base_columns
              <django_tables2.columns.base.Column at 0x7f78f8033730>)])

With this example the table renders correctly but it doesn’t exclude any fields from the Client model. Docs

class ClientTable(tables.Table):
    class Meta:
        model = Client
        exclude = ("id", )
        # Also tried with fields = ("name", "surname", )

class ClientListView(tables.SingleTableView):
    model = Client
    table_class = ClientTable
    template_name = "clients/clients.html"

Thanks in advance!

From the tutorial, it is fields that you need to use, not exclude.

Also, notice the required change to the template referenced in the text below the example. Did you adjust your template as identified?

It seems I skipped the part where it says you must render a table to allow customization. :man_facepalming:

Btw, it also works with exclude = ("fieldname", )
Just be sure it’s a tuple. Source

Thank you so much Ken!

1 Like