Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot guillaumepiot/
Last active Nov 16, 2016

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,]

This comment has been minimized.

Copy link

commented Nov 5, 2015

Do you define _ as:

from django.utils.translation import gettext as _



This comment has been minimized.

Copy link

commented Nov 16, 2016

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
You can’t perform that action at this time.