7.2. Admin List
Change List customization in Django Admin
list_displaylist_display_linkslist_filterorderingsearch_fieldslist_per_pageCustom field display
7.2.1. SetUp
>>> from django.contrib import admin
>>> from django.utils.translation import gettext_lazy as _
>>> from myapp.models import MyModel
7.2.2. list_display
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_display = ['lastname', 'firstname', 'birthdate']
7.2.3. list_display_links
Django by default uses the first field in list_display as the link to the change page
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_display_links = ['lastname', 'firstname']
7.2.4. list_filter
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_filter = ['birthdate', 'gender']
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_filter = ['birthdate', 'gender']
...
... def get_list_filter(self, request):
... fields = super().get_list_filter(request)
... if request.user.is_superuser:
... fields = ['is_active'] + fields
... return fields
7.2.5. ordering
-fieldname- descending order
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... ordering = ['-lastname', 'firstname']
7.2.6. search_fields
search_fields^- starts with=- exactly@- contains
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... search_fields = ['lastname', 'firstname']
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... search_fields = ['^customer__lastname', '@street', '=postcode']
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... search_fields = ['^name', '=barcode']
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... search_fields = ['^customer__lastname', '^product__name']
7.2.7. list_per_page
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_per_page = 100
7.2.8. Custom Field Display
>>> #
... @admin.register(MyModel)
... class MyAdmin(admin.ModelAdmin):
... list_display = ['firstname', 'lastname', 'display_emails']
...
... @admin.display(empty_value='', description=_('Email'))
... def display_emails(self, model):
... result = model.emails.values_list('email', flat=True)
... return ', '.join(result)
7.2.9. Assignments
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList List Display
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` model display the following fields:
# - `is_verified`
# - `lastname`
# - `firstname`
# - `birthdate`
# - `age` (classmethod from model)
# - `email`
# - `phone`
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` wyświetl następujące pola:
# - `is_verified`
# - `lastname`
# - `firstname`
# - `birthdate`
# - `age` (metoda klasowa z modelu)
# - `email`
# - `phone`
# %% Hints
# - `admin.ModelAdmin`
# - `list_display = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList List Display Links
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` model display links on the following fields:
# - `lastname`
# - `firstname`
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` wyświetl linki na następujących polach:
# - `lastname`
# - `firstname`
# %% Hints
# - `admin.ModelAdmin`
# - `list_display_links = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList List Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` model add filtering by:
# - `gender`
# - `is_verified`
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` dodaj filtrowanie po:
# - `gender`
# - `is_verified`
# %% Hints
# - `admin.ModelAdmin`
# - `list_filter = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList Ordering
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` order by:
# - `lastname` (ascending)
# - `firstname` (descending)
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` posortuj według:
# - `lastname` (rosnąco)
# - `firstname` (malejąco)
# %% Hints
# - `admin.ModelAdmin`
# - `ordering = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList Search Fields
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` add search by:
# - `lastname` (starts with)
# - `tax_id` (exact)
# - `email` (contains)
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` dodaj wyszukiwanie po:
# - `lastname` (zaczyna się od)
# - `tax_id` (dokładne dopasowanie)
# - `email` (zawiera)
# %% Hints
# - `admin.ModelAdmin`
# - `search_fields = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList List Per Page
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person` limit number of items per page to `100`
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person` ogranicz liczbę elementów na stronę do `100`
# %% Hints
# - `admin.ModelAdmin`
# - `list_per_page = [...]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result
# doctest: +SKIP_FILE
# %% About
# - Name: Admin ChangeList Custom Field
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 0. Use `myproject.myapp`
# 1. In admin list view for `myapp.Person`
# add custom field `fullname` by combining `firstname` and `lastname`
# 2. Set column header to `Full Name`
# 3. Set sorting by `lastname` and `firstname`
# 4. Set empty values to '' (empty string)
# %% Polish
# 0. Użyj `myproject.myapp`
# 1. W widoku listy admin dla modelu `myapp.Person`
# dodaj pole niestandardowe `fullname` łącząc `firstname` i `lastname`
# 2. Ustaw nagłówek kolumny na `Full Name`
# 3. Ustaw sortowanie po `lastname` i `firstname`
# 4. Ustaw puste wartości na '' (pusty ciąg znaków)
# %% Hints
# - `admin.ModelAdmin`
# - `@admin.display()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
# %% Types
# %% Data
# %% Result