liveBook · Manning

List_display

As explained in Django in Action

list_display is a Django ModelAdmin option that specifies the fields or callables rendered as columns on an administration listing page.

list_display controls the columns shown for each object on a Django Admin listing page. Give it an iterable, usually a tuple, containing model field names, argument-less model methods, or callables on the ModelAdmin class. Each callable supplies the value displayed in its column. section 5.2

Show a musician’s birth weekday

In RiffMates, this configuration replaces the default object display with an ID, surname, and computed weekday:\n\nclass MusicianAdmin(admin.ModelAdmin):\n list_display = ('id', 'last_name', 'show_weekday')\n\n def show_weekday(self, obj):\n return obj.birth.strftime("%A")\n\n show_weekday.short_description = "Birth Weekday"\n\nDjango uses the first two names as Musician fields, calls show_weekday once per row, and uses its return value for the custom column. The method belongs on MusicianAdmin when the display feature is only needed in the Admin. section 5.2

Listing 5.2 Customized MusicianAdmin class with three columns
# RiffMates/bands/admin.py
from django.contrib import admin
from bands.models import Musician

@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
    list_display = ('id', 'last_name', 'show_weekday')  #1

    def show_weekday(self, obj):  #2
        # Fetch weekday of artist’s birth
        return obj.birth.strftime("%A")  #3
    show_weekday.short_description = "Birth Weekday"  #4

Inspect the complete MusicianAdmin configuration, including the three-column tuple, row callable, formatted weekday, and custom heading. section 5.2

How Django in Action covers it

How deep it goes
Defines fields and callable behavior, including per-row values and callable headings, but does not detail broader listing-page configuration.
Examples
Uses MusicianAdmin with id, last_name, and a show_weekday callable returning an artist’s birth weekday.
What it assumes
Assumes a registered model and familiarity with admin.ModelAdmin, while demonstrating callable signatures from the example.
What you can do after
Build it from scratch by configuring model fields and custom callables as columns on an Admin listing page.
How it connects
Connects Django Admin listings with ModelAdmin classes, model fields, model methods, callables, sorting, search, and filters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.