I have a Blog model and an Entry model, following the example in django's documentation.
Entry has a ForeignKey to Blog: one Blog has several Entries.
I have two FieldListFilters for Blog: one for "Entry title", one for "Entry published year".
If in the Blog list admin page I filter for both entry__title='Lennon'
and entry__published_year=2008
, then I see all Blogs which have at least one entry with title "Lennon" and at least one entry from 2008. They do not have to be the same entry.
However, that's not what I want. What I want is to filter blogs which have entries that have both got the title "Lennon" and are from 2008.
So for example say I have this data:
Blog | Entry Title | Entry year |
---|---|---|
A | McCartney | 2008 |
A | Lennon | 2009 |
B | Lennon | 2008 |
The admin list page for Blog currently filters in Blog A, because it has one entry from 2008 and one entry for "Lennon", as well as Blog B. I only want to see Blog B.
This is because django does this when it builds the queryset:
qs = qs.filter(title_filter)
qs = qs.filter(published_filter)
As per the docs, to get the desired result it would need to make just one filter call:
qs = qs.filter(title_filter & published_filter)
How can I achieve this behaviour with filtering in the admin?
Background:
Both filters are different concerning filtering on many-to-many relationships. See above link to the docs.
MyModel.filter(a=b).filter(c=d)
MyModel.filter(a=b, c=d)