← Back to Django Course | Chapter 8: Django Admin | Lesson 5 of 8

Admin Search and Filters

search_fields adds a search box, and list_filter adds a sidebar so you can quickly find records in a long admin list.

Adding a Search Box

Setting search_fields on a ModelAdmin puts a search box above the list page that performs a case-insensitive partial match against every listed field.

Example: Adding a Search Box

Typing into the search box now matches against Book.title and the related Author's name field.

markup
from django.contrib import admin
from .models import Book

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    search_fields = ('title', 'author__name')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Adding Filter Sidebar Options

list_filter adds a sidebar of clickable filters. It works best on fields with a small, fixed set of values like booleans, choices, or dates.

Example: Adding Filter Sidebar Options

Adds sidebar filters for is_published (True/False) and published_date (by year/month/day).

markup
class BookAdmin(admin.ModelAdmin):
    list_filter = ('is_published', 'published_date')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Combining Search and Filters

search_fields and list_filter work together and independently -- a search query and an active filter both narrow the same list at once.

Warning: list_filter on a field with many unique values (like a free-text title) makes the sidebar impractically long; prefer search_fields for those.

Example: Combining Search and Filters

Gives Book a searchable, filterable admin list in a single ModelAdmin class.

markup
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'is_published')
    search_fields = ('title',)
    list_filter = ('is_published',)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Putting a ForeignKey field directly in search_fields instead of using the double-underscore lookup into a related field.
  2. Adding list_filter on a field with hundreds of distinct values, making the sidebar unusably long.
  3. Expecting search_fields to search every field automatically -- it only searches the fields you list.
Chapter Summary
  • search_fields adds a search box above the admin list that matches against the listed fields.
  • list_filter adds a right-hand sidebar with clickable filters for the listed fields.
  • Related fields can be searched with double-underscore syntax, e.g. author__name.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.