Начиная с версии 1.4, выпущенной 23 марта 2012 г., вы можете использовать официальный django.contrib.admin.SimpleListFilter
Вот пример кода фильтра, используемого только в этом списке активных компаний:
class ActiveCompaniesFilter(SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('active companies')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'project__company'
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
lookup_list = Company.objects.active().values_list('id', 'name').distinct()
# 'objects.active' is a custom Company manager and is
# equivalent to filter 'objects.filter(status=100)'
return lookup_list
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
# Compare the requested value (either '80s' or 'other')
# to decide how to filter the queryset.
if self.value():
return queryset.filter(project__company=self.value())