0
0
Djangoframework~5 mins

List display configuration in Django

Choose your learning style9 modes available
Introduction

List display configuration helps you decide what information shows up in the admin list view. It makes it easy to see important details at a glance.

You want to see specific fields of your data in the admin list page.
You want to quickly edit or check multiple records without opening each one.
You want to organize the list view to show the most useful information first.
Syntax
Django
class ModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'method_name')

list_display is a tuple of field names or method names to show as columns.

Methods used in list_display must be defined inside the ModelAdmin class and return a string or value.

Examples
Shows three fields from the Book model in the admin list.
Django
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_date')
Shows a custom column 'is_recent' that returns True if the book was published in 2020 or later.
Django
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'is_recent')

    def is_recent(self, obj):
        return obj.published_date.year >= 2020
    is_recent.boolean = True
Shows no columns in the list view (not useful but valid).
Django
class EmptyAdmin(admin.ModelAdmin):
    list_display = ()
Sample Program

This example shows a Book model with three fields. The admin list view will show the title, author, and a custom column for the year published extracted from the date.

Django
from django.contrib import admin
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_year')

    def published_year(self, obj):
        return obj.published_date.year
    published_year.short_description = 'Year Published'

admin.site.register(Book, BookAdmin)
OutputSuccess
Important Notes

Time complexity: Displaying list columns is fast because it uses database queries optimized by Django.

Space complexity: Minimal extra memory is used for displaying columns.

Common mistake: Forgetting to add the method to list_display or not returning a value causes errors.

Use list_display to show important fields; use list_filter or search_fields for filtering instead.

Summary

list_display controls which fields or methods show as columns in the admin list view.

You can add custom methods to show computed or formatted data.

It helps you quickly see and manage your data in the admin interface.