Admin Search and Filters
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.
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).
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.
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. #}
- Putting a ForeignKey field directly in search_fields instead of using the double-underscore lookup into a related field.
- Adding list_filter on a field with hundreds of distinct values, making the sidebar unusably long.
- Expecting search_fields to search every field automatically -- it only searches the fields you list.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: