Search code examples
django-querysetdjango-tables2

Should I use singletableview?


I'm learning about Django tables. I first wrote a basic example, here my view:

def people1(request):
    table = PersonTable(Person.objects.filter(id=2))
    RequestConfig(request).configure(table)
    return render(request, 'people.html', {'table': table})

This way I've been able to easily display a table with a filter condition "filter(id=2))".

After that I found SingleTableView which is supposed to be an easier way to display database tables, as an example I wrote this view, which worked fine:

from django_tables2 import SingleTableView
class PersonList(SingleTableView):
    template_name = 'ta07/comun.html'
    model = Person
    table_class = PersonTable

Questions are: how should I do to apply filters like in the first example? And is SingleTableView better than the basic way?


Solution

  • I'd say for now, you should only use it for the very basic use case. As soon as you need customizations from that, use your own.

    Since filtering is a very common use case, I might consider adding that to the features of SingleTableView at some point. If you need it before that, feel free to open a pull request.