🎓 A1Learner
🔍
0 of 50 lessons completed

Django Admin List Display

List display picks the columns on the admin change list. The list shows one row per saved object.

Make the admin class

The class names the fields to show. Title becomes a column on the list.

class PostAdmin(admin.ModelAdmin):
    list_display = ["title"]

Register with the class

Register takes the model and the admin class together. The class shapes the list.

admin.site.register(Post, PostAdmin)

Ask for the post list

The change list shows the title column. Both saved titles appear on the page.

client = Client()
client.login(username="sana", password="secret123")
response = client.get("/admin/blog/post/")
print(response.status_code)
print("First post" in response.content.decode())
print("Second post" in response.content.decode())

Example

import django
from django.apps import AppConfig
from django.conf import settings

class BlogConfig(AppConfig):
    name = "__main__"
    label = "blog"

settings.configure(
    DEBUG=True,
    SECRET_KEY="key",
    ROOT_URLCONF=__name__,
    ALLOWED_HOSTS=["*"],
    INSTALLED_APPS=[
        "__main__.BlogConfig",
        "django.contrib.admin",
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "django.contrib.sessions",
        "django.contrib.messages",
        "django.contrib.staticfiles",
    ],
    MIDDLEWARE=[
        "django.middleware.security.SecurityMiddleware",
        "django.contrib.sessions.middleware.SessionMiddleware",
        "django.middleware.common.CommonMiddleware",
        "django.middleware.csrf.CsrfViewMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.messages.middleware.MessageMiddleware",
    ],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
    TEMPLATES=[{"BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [], "APP_DIRS": True, "OPTIONS": {"context_processors": ["django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages"]}}],
    STATIC_URL="/static/",
)
django.setup()

from django.contrib import admin
from django.contrib.auth.models import User
from django.core.management import call_command
from django.db import connection, models
from django.urls import path

class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()

    class Meta:
        app_label = "blog"

with connection.schema_editor() as editor:
    editor.create_model(Post)
call_command("migrate", verbosity=0)
User.objects.create_superuser("sana", "sana@example.com", "secret123")

from django.test import Client

class PostAdmin(admin.ModelAdmin):
    list_display = ["title"]

admin.site.register(Post, PostAdmin)
urlpatterns = [path("admin/", admin.site.urls)]
Post.objects.create(title="First post", body="Hello from Django.")
Post.objects.create(title="Second post", body="Models save rows.")

client = Client()
client.login(username="sana", password="secret123")
response = client.get("/admin/blog/post/")
print(response.status_code)
print("First post" in response.content.decode())
print("Second post" in response.content.decode())

Key points

  • A ModelAdmin class holds the settings for one model in the admin.
  • Set list_display to the list of fields to show as columns.
  • List display names model fields, like ["title"].
  • Register the model with its ModelAdmin class.
  • The change list shows the named fields for every row.

In this example, the setup lines configure Django. The blog app config gives the model an app to live in. The program creates the table directly. A real project uses migrations for this step. PostAdmin names title as the one column. Register ties the model to its admin class. Migrate builds the tables, and createsuperuser makes the admin account. Two create calls save two posts. The test client logs in and asks for the post list. The prints show 200 and that both titles appear on the page.

Note

Without list display, the list shows the object name, like Post object (1).

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does list_display = ["title"] do?