Django CookBook: Custom Admin List Filters

This snippet will create a custom admin filter should you wish to use your own display options and a custom query per option.

# Tested in Django 1.6
# Import default list filter
from django.contrib.admin import SimpleListFilter
# Create the filter
class InvoicePaidFilter(SimpleListFilter):
    title = _('Paid')
    parameter_name = 'paid'
    # Set the displaying options
    def lookups(self, request, model_admin):
        return (
            ('PAID', _('Paid')),
            ('UNPAID', _('Unpaid')),
        )
    # Assign a query for each option
    def queryset(self, request, queryset):
        if self.value() == 'PAID':
            return queryset.filter(paid=True)
        elif self.value() == 'UNPAID':
            return queryset.exclude(paid=True)
# Assign the filter
class InvoiceAdmin(admin.ModelAdmin):
    list_filter = [InvoicePaidFilter,]
    ...

View Gist

< / >