Alvaro
Alvaro

Reputation: 12037

Django chaining filters doesn't work

I'm trying to filter a ListView and I'm having some trouble chaining the filters.

This is what I have tried so far:

accounts = Accounts.objects.all()
if self.filter_form.cleaned_data['type']:
    accounts.filter(type=self.filter_form.cleaned_data['type'])

However the filter doesn't seem to chain and I end up with all the objects. I tried printing the query but it never changes. Am I doing something wrong? Can't filters be chained like this?

Upvotes: 0

Views: 97

Answers (1)

Justin O Barber
Justin O Barber

Reputation: 11591

accounts still refers to Accounts.objects.all(). You need to reassign the name in order to point to the filtered query:

accounts = accounts.filter(type=self.filter_form.cleaned_data['type'])

Upvotes: 3

Related Questions