Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DJANGO CookBook - Custom list filter
# 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,]

bdpdx commented Nov 5, 2015

Do you define _ as:

from django.utils.translation import gettext as _


How can I display the count of records in front of each filter?
i.e display in list filter

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