Skip to content

Instantly share code, notes, and snippets.

@acdha
Created August 31, 2012 20:14
Show Gist options
  • Save acdha/3558327 to your computer and use it in GitHub Desktop.
Save acdha/3558327 to your computer and use it in GitHub Desktop.
Django admin list_filter which filters out values which are not used in the current queryset
from django.contrib.admin import FieldListFilter
from django.utils.translation import ugettext as _
class ActiveValueFilter(FieldListFilter):
"""list_filter which displays only the values for a field which are in use
This is handy when a large range has been filtered in some way which means
most of the potential values aren't currently in use. This requires a
queryset field or annotation named "item_count" to be present so it can
simply exclude anything where item_count=0.
Usage::
class MyAdmin(ModelAdmin):
list_filter = (('title', ActiveValueFilter))
"""
def __init__(self, field, request, params, model, model_admin, field_path):
self.lookup_kwarg = '%s__exact' % field_path
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
super(ActiveValueFilter, self).__init__(field, request, params,
model, model_admin,
field_path)
qs = model_admin.queryset(request)
qs = qs.exclude(item_count=0)
qs = qs.order_by(field.name).values_list(field.name, flat=True)
self.active_values = qs.distinct()
def expected_parameters(self):
return [self.lookup_kwarg]
def choices(self, cl):
yield {
'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')
}
for lookup in self.active_values:
yield {
'selected': lookup == self.lookup_val,
'query_string': cl.get_query_string({
self.lookup_kwarg: lookup}),
'display': lookup,
}
@wjurkowlaniec
Copy link

well, 'StuffAdmin.list_filter[1]' is 'ActiveValueFilter' which is of type FieldListFilter but is not associated with a field name.
Do you know how to fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment