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

Customizing the Admin list_display

list_display decides which columns show up in the admin's table view of your records.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Putting a many-to-many field directly in list_display, which Django doesn't allow and raises an error for.
  2. Listing field names that don't exist on the model, causing an admin startup error.
  3. Forgetting that list_display only changes the list page, not the individual add/edit form.
Chapter Summary
  • 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.

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.