Customizing the Admin list_display
The Default List View
Without any customization, the admin's list page for a model shows one column: whatever __str__() returns for each row. That's rarely enough detail at a glance.
Example: The Default List View
With no ModelAdmin customization, Book's list page only shows the __str__ value per row.
from django.contrib import admin
from .models import Book
admin.site.register(Book)
{# 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 Columns with list_display
Setting list_display on a ModelAdmin class adds one column per named field, turning the list page into a proper table.
Example: Adding Columns with list_display
The Book list page now shows title, author, and published_date as separate columns.
from django.contrib import admin
from .models import Book
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', '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. #}
Showing a Computed Value
A method defined on the ModelAdmin (or the model itself) can also appear in list_display, letting you show derived information that isn't a plain field.
Note: Give the method a short_description attribute to control its column header text.
Example: Showing a Computed Value
Adds an 'Recent?' column computed from published_date instead of a stored field.
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'is_recent')
def is_recent(self, obj):
return obj.published_date.year >= 2020
is_recent.short_description = 'Recent?'
{# 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 many-to-many field directly in list_display, which Django doesn't allow and raises an error for.
- Listing field names that don't exist on the model, causing an admin startup error.
- Forgetting that list_display only changes the list page, not the individual add/edit form.
- list_display is a tuple of field names (or method names) shown as columns in the admin list page.
- It defaults to just showing the model's __str__ output as a single column if left unset.
- Callables and model methods can appear in list_display alongside plain field names.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: