I've got a question - can Django Admin interface be customized in such a way, that it presents only those filter options that occur in the subset of data matching currently selected filters?
Say I have a db of three objects:
a.Foo = "One"
a.Bar = "Drink"
b.Foo = "One"
b.Bar = "Shot"
c.Foo = "Two"
c.Bar = "Shot"
And a django admin interface with filters on 'Foo' and 'Bar'. I want the following behavior:
- If no filters are chosen, 'Foo' lists "One","Two"; 'Bar' lists "Drink", "Shot"
- If 'Foo' filter is set to "One", 'Bar' lists both "Drink" and "Shot"
- If 'Foo' filter is set to "Two", 'Bar' lists only "Shot"
- If 'Bar' filter is set to "Shot", 'Foo' lists both "One" and "Two"
- If 'Bar' filter is set to "Drink", 'Foo' lists only "One"
Cheers!
To be more specific - after reading some docs:
from django.contrib.admin import SimpleListFilter
class SomeFilter(SimpleListFilter):
title = "Foo"
parameter_name="Some"
def lookups(self, request, model_admin):
qs = model_admin.queryset(request)
print qs.query
return (('Foo', 'Bar'))
def queryset(self, request, queryset):
if (self.value()):
return queryset.filter(Some=self.value())
else:
return queryset
What it does, however, is gets the 'queryset' as it would've been without other filters. How can I pass it through other filters?
I could theoretically parse the request and filter manually - but there surely needs to be a way to pipe all filters.