Search code examples
djangopython-3.xdjango-class-based-viewsdjango-tables2django-filters

how to use django-filters on django-tables2


This is my view:

class PersonalView(SingleTableMixin, FilterView):
    model = Invoice
    template_name = 'invProj/index.html'
    table_class = InvoiceTable
    filterset_class = InvoiceFilter
    context_object_name = 'invoice'
    ordering = ['invoice_due_date']

    def get_table_data(self):
        return Invoice.objects.filter(invoice_owner__username=self.request.user).order_by('i
nvoice_due_date')

Now, get_table_data does the right thing, the invoices are filtered according to the user. But, the InvoiceFilter(django_filters.FilterSet) does not work then. It does work, however, when I don't override get_table_data.

Right now, the filters, which appear as normal and are passed normally, just don't filter. It always shows the data according to get_table_data, no matter which filter string I enter.

How can I get it all? I want define my custum table data and have my defined filters work on that.


Solution

  • OK now, I figured it out myself...

    Instead of overriding get_table_data, I had to override get_queryset , like so

    def get_queryset(self):
        qs = Invoice.objects.filter(invoice_owner__username=self.request.user).order_by('invoice_due_date')
        return qs