Search code examples
djangodjango-tables2

django-tables2 exclude & field not working


I'm new to django and stumbling through creating my first site. I'm using django-tables2 to display a table and it appears to be working (the table shows up, it's sortable).

Except I can't seem to customize anything. Exclude, field and sequence don't work. Can't change column verbose names.

TABLE:

import django_tables2 as tables
from sl_overview.models import DailyslSumm

class slsummTable(tables.Table):

    class Meta:
        model = DailyslSumm
        exclude = ('index')

VIEW:

class sl_summ(SingleTableView):

    model = DailyslSumm
    context_object_name = 'slsummdb'
    table_class = slsummTable

TEMPLATE:

{% load render_table from django_tables2 %}
{% render_table slsummdb %}

The exclude in the code above doesn't work. The column is still there. Using field doesn't adjust columns either. I'm sure I'm missing something simple, thanks for any help.


Solution

  • You must make sure exclude is a tuple (or list), and not a string. If you use parenthesis with one string, the resulting value will be a string, not a tuple like you might expect:

    Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
    >>> ('foo')
    'foo'
    >>> ('foo', )
    ('foo',)
    >>> 
    

    In your case, you should add a comma after 'index' like this:

    class slsummTable(tables.Table):
    
        class Meta:
            model = DailyslSumm
            exclude = ('index', )  # <- note the extra comma here