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.
List display configuration in 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.
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'author', 'published_date')
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
class EmptyAdmin(admin.ModelAdmin):
list_display = ()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.
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)
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.
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.
Practice
list_display attribute control?Solution
Step 1: Understand the purpose of
Thelist_displaylist_displayattribute in Django admin defines which model fields or methods are shown as columns in the list view of the admin interface.Step 2: Differentiate from other admin attributes
Other attributes likeorderingcontrol order,fieldscontrol form fields, and database table names are unrelated tolist_display.Final Answer:
Which fields or methods appear as columns in the admin list view -> Option AQuick Check:
list_display = columns shown [OK]
- Confusing list_display with ordering or form fields
- Thinking list_display changes database schema
- Assuming list_display controls permissions
list_display in a Django admin class?Solution
Step 1: Identify the correct data type for list_display
Thelist_displayattribute expects a list or tuple of strings naming fields or methods. Both list and tuple are valid, but list is most common.Step 2: Analyze each option's syntax
list_display = ['field1', 'field2', 'method_name'] uses a list with square brackets and strings, which is correct. list_display = (field1, field2, method_name) uses tuple syntax but lacks quotes around the names, treating them as undefined variables (NameError). list_display = {'field1', 'field2', 'method_name'} uses a set, which is unordered and invalid here. list_display = 'field1, field2, method_name' is a string, which is incorrect.Final Answer:
list_display = ['field1', 'field2', 'method_name'] -> Option DQuick Check:
list_display uses list syntax [OK]
- Using a string instead of a list or tuple
- Using a set which is unordered
- Forgetting quotes around field names
class ProductAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'discounted_price']
def discounted_price(self, obj):
return obj.price * 0.9Solution
Step 1: Understand list_display with methods
Thelist_displayincludes 'discounted_price', which is a method defined in the admin class. Django calls this method for each object to show the computed value.Step 2: Analyze the method's return value
The method returns 90% of the product's price, so the column will show the discounted price correctly.Final Answer:
Columns: name, price, discounted_price showing name, price, and 90% of price -> Option CQuick Check:
Method in list_display shows computed column [OK]
- Assuming methods in list_display are ignored
- Forgetting to define the method in admin
- Method returning wrong or no value
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'total_price', 'calculate_tax']
def calculate_tax(self):
return self.total_price * 0.1Solution
Step 1: Check method signature for list_display methods
Methods used inlist_displaymust accept the model instance as the first parameter, usually namedobj. Here,calculate_taxhas no parameters, so it will cause an error.Step 2: Confirm other options
Methods can be in list_display, total_price is assumed valid, and methods do not have to be properties.Final Answer:
The method calculate_tax is missing the obj parameter -> Option BQuick Check:
list_display methods need obj parameter [OK]
- Defining method without obj parameter
- Thinking methods can't be in list_display
- Confusing method with property decorator
list_display?Solution
Step 1: Create a method in admin for custom display
To show a custom column based on logic, define a method in the admin class that takesselfandobjand returns the desired string.Step 2: Add the method name as a string to list_display
Adding the method name tolist_displaytells Django to call it for each row and show the result.Final Answer:
Define a method in admin:def in_stock(self, obj): return 'Yes' if obj.stock > 0 else 'No'and add 'in_stock' to list_display -> Option AQuick Check:
Custom columns need admin methods + list_display [OK]
- Adding method name without defining it
- Trying to use model __str__ for columns
- Assuming properties on model auto-show in list_display
Ready to prove it?
Pass the test and this pattern moves unknown
