Django Set List Display
Customize admin list view columns
📋 What is List Display?
List display controls which model fields appear as columns in the Django admin list view. By default, only the __str__ method output shows. Customize it to display multiple fields for better data overview.
# admin.py
class MemberAdmin(admin.ModelAdmin):
list_display = ('firstname', 'lastname', 'email')
admin.site.register(Member, MemberAdmin)
List Display Features
Multiple Columns
Show several fields at once
list_display = ('name', 'email')
Related Fields
Display foreign key data
list_display = ('book', 'author')
Custom Methods
Add calculated columns
def full_name(self, obj):
Formatting
Style column output
colored_status.admin_order_field
🔹 Basic List Display
Set list_display to show specific model fields as columns in the admin interface. This tuple defines which fields appear and their order from left to right in the admin list view.
# myapp/admin.py
from django.contrib import admin
from .models import Member
class MemberAdmin(admin.ModelAdmin):
list_display = ('firstname', 'lastname', 'email', 'phone', 'joined_date')
admin.site.register(Member, MemberAdmin)
Result in Admin:
The admin list page will show a table with columns: Firstname, Lastname, Email, Phone, and Joined Date
🔹 Display Related Fields
Access fields from related models using double underscore notation. This allows you to show data from foreign key relationships directly in the list view without additional queries or custom methods.
# models.py
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=6, decimal_places=2)
# admin.py
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
# Display author name from related model
list_display = ('title', 'author__name', 'price')
admin.site.register(Book, BookAdmin)
🔹 Custom Display Methods
Create custom methods to display calculated or formatted data in the admin list. These methods receive the model instance and can return any string or HTML content for display in the column.
# myapp/admin.py
from django.contrib import admin
from .models import Member
class MemberAdmin(admin.ModelAdmin):
list_display = ('full_name', 'email', 'membership_duration')
def full_name(self, obj):
return f"{obj.firstname} {obj.lastname}"
full_name.short_description = 'Full Name'
def membership_duration(self, obj):
from django.utils import timezone
days = (timezone.now().date() - obj.joined_date).days
return f"{days} days"
membership_duration.short_description = 'Member For'
admin.site.register(Member, MemberAdmin)
🔹 Boolean Icons
Django automatically displays boolean fields with green checkmark or red X icons. You can also create custom methods that return boolean values to get the same visual treatment in the admin interface.
# myapp/admin.py
from django.contrib import admin
from .models import Member
class MemberAdmin(admin.ModelAdmin):
list_display = ('firstname', 'lastname', 'is_active', 'is_premium')
def is_premium(self, obj):
# Returns True/False, displays as icon
return obj.membership_type == 'premium'
is_premium.boolean = True
is_premium.short_description = 'Premium Member'
admin.site.register(Member, MemberAdmin)
🔹 Colored Status Display
Add HTML formatting to display columns with colors or custom styling. Use format_html to safely inject HTML into admin columns, making status indicators and important information more visually prominent.
# myapp/admin.py
from django.contrib import admin
from django.utils.html import format_html
from .models import Order
class OrderAdmin(admin.ModelAdmin):
list_display = ('order_id', 'customer', 'colored_status', 'total')
def colored_status(self, obj):
colors = {
'pending': 'orange',
'completed': 'green',
'cancelled': 'red',
}
color = colors.get(obj.status, 'black')
return format_html(
'{}',
color,
obj.status.upper()
)
colored_status.short_description = 'Status'
admin.site.register(Order, OrderAdmin)
🔹 Sortable Columns
Make custom method columns sortable by specifying which database field to use for ordering. This enables users to click column headers to sort the list by that column's values.
# myapp/admin.py
from django.contrib import admin
from .models import Member
class MemberAdmin(admin.ModelAdmin):
list_display = ('full_name', 'email', 'joined_date')
def full_name(self, obj):
return f"{obj.firstname} {obj.lastname}"
full_name.short_description = 'Full Name'
# Make it sortable by firstname
full_name.admin_order_field = 'firstname'
admin.site.register(Member, MemberAdmin)