Skip to content

List Columns, Filters & Search

If we want to find an animal that just arrived today, we don't want to scroll through a long list of names.

ModelAdmin has settings that control how the list page displays.

List Columns with list_display

We want to see more details about each animal without clicking through to the details page. Replace pass with the list of fields to show:

animals/admin.py
@admin.register(Animal)
class AnimalAdmin(admin.ModelAdmin):
    list_display = ["name", "species", "breed", "status", "intake_date"]

Each field in list_display becomes a column.

With the page reloaded, click a column's header to sort.

The species and status columns show the readable label of each choice, such as "Available", and not the value stored in the database, available.

Filters with list_filter

You might want to only see cats, or filter by animals that are still available to adopt.

animals/admin.py
    list_display = ["name", "species", "breed", "status", "intake_date"]
    list_filter = ["species", "status"]

The list page now has a sidebar of links, built from each field's choices.

Click Cat. Only the cats are listed, and the address ends in ?species__exact=cat. That's the lookup filter(species="cat") uses.

Search with search_fields

To find one animal by name, we need a search box. search_fields lists the fields it searches through:

animals/admin.py
    list_display = ["name", "species", "breed", "status", "intake_date"]
    list_filter = ["species", "status"]
    search_fields = ["name", "breed"]

The admin searches each of those fields with the icontains lookup, a case-insensitive "contains", and keeps a row when any of them matches.

Search for calico. The calico cats are listed, matched by their breed.

Tip

An entry can follow a foreign key with two underscores, as filter(animal__species="cat") does. On AdoptionApplication, animal__name searches the name of each application's animal.