liveBook · Manning

SimpleListFilter

As explained in Django in Action

SimpleListFilter is a Django Admin base class for defining custom sidebar filters with selectable lookup values and QuerySet filtering behavior.

SimpleListFilter customizes a Django Admin sidebar filter when the default field values are too broad or unhelpful. It supplies selectable lookup links and applies the chosen value to the page’s QuerySet. section 5.2.2

A custom filter combines two attributes with two methods:

  • Set title for the sidebar heading and parameter_name for the URL query parameter. section 5.2.2
  • Implement lookups(request, model_admin) to return short URL values paired with user-facing labels. section 5.2.2
  • Implement queryset(request, queryset) to read self.value(), preserve the original QuerySet when no choice is selected, and filter otherwise. section 5.2.2

Filtering musicians by birth decade

DecadeListFilter extends admin.SimpleListFilter with title = 'decade born' and parameter_name = 'decade'. Its lookups() creates decade choices from the current decade backward; queryset() converts the selected value to a year and filters birth__gte and birth__lte. With no selection, it returns the unfiltered QuerySet. section 5.2.2

Listing 5.4 Custom list filter based on decades
# RiffMates/bands/admin.py
...
from datetime import datetime, date  #1
...
class DecadeListFilter(admin.SimpleListFilter):  #2
    title = 'decade born'     #3
    parameter_name = 'decade'  #4

    def lookups(self, request, model_admin):
        result = []

        this_year = datetime.today().year
        this_decade = (this_year // 10) * 10   #5
        start = this_decade - 10
        for year in range(start, start - 100, -10):  #6
            result.append( (str(year), f"{year}-{year+9}") )  #7

        return result

    def queryset(self, request, queryset):
        start = self.value()   #8
        if start is None:
            return queryset

        start = int(start)
        result = queryset.filter(
            birth__gte=date(start, 1, 1),  #9
            birth__lte=date(start + 9, 12, 31),
        )

        return result
...
class MusicianAdmin(admin.ModelAdmin):
    list_display = ("id", "last_name", "first_name", "birth",   #10
        "show_weekday", )
    list_filter = (DecadeListFilter, )  #11

Inspect the complete decade-based admin filter, including dynamic lookup generation and date-range filtering. section 5.2.2

How Django in Action covers it

How deep it goes
Defines two attributes and two methods, then demonstrates lookup generation and QuerySet filtering without descending into framework internals.
Examples
Demonstrates DecadeListFilter for RiffMates musicians, generating decade links and filtering birthdays between calendar-year boundaries.
What it assumes
Assumes familiarity with Django Admin models and QuerySets while demonstrating the filter class structure and method roles.
What you can do after
Build it from scratch by defining filter labels, generating selectable values, and applying the selected range to an admin QuerySet.
How it connects
Connects Django Admin, ModelAdmin, QuerySet filtering, field choices, and URL query parameters.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.