0 day streak
python / expert
Snippet

Django Admin Advanced Customization with ModelAdmin Hooks and List Display

Django's ModelAdmin class provides extensive customization hooks for the admin interface. list_display defines which columns appear in the list view, including custom methods that format data. list_filter creates a sidebar with filtering options by specified fields. search_fields enables global search across specified model and related fields using __ notation for lookups. readonly_fields marks fields as non-editable. fieldsets groups fields into collapsible sections. Custom method attributes like short_description and admin_order_field control column header display and sorting behavior. The format_html function safely renders HTML in admin templates.

snippet.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.contrib import admin
from django.utils.html import format_html
from django.utils import timezone
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'author_name', 'status_badge', 'created_at', 'word_count_display')
list_filter = ('status', 'author', 'created_at')
search_fields = ('title', 'content', 'author__name')
readonly_fields = ('created_at', 'updated_at', 'word_count_display')
date_hierarchy = 'created_at'
ordering = ('-created_at',)
fieldsets = (
('Content', {'fields': ('title', 'slug', 'content', 'summary')}),
('Metadata', {'fields': ('author', 'status', 'published_at')}),
('System', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
)
def author_name(self, obj):
return obj.author.name
author_name.short_description = 'Author'
author_name.admin_order_field = 'author__name'
def status_badge(self, obj):
colors = {'draft': '#6c757d', 'published': '#28a745', 'archived': '#dc3545'}
return format_html(
'<span style="background:{};color:white;padding:3px 8px;border-radius:3px">{}</span>',
colors.get(obj.status, '#6c757d'), obj.status.upper()
)
status_badge.short_description = 'Status'
def word_count_display(self, obj):
return f"{len(obj.content.split())} words"
word_count_display.short_description = 'Word Count'
django
Breakdown
1
list_display = ('title', 'author_name', 'status_badge', ...)
Defines columns shown in list view; custom methods render custom columns
2
list_filter = ('status', 'author', 'created_at')
Creates sidebar filter widgets for specified fields
3
search_fields = ('title', 'content', 'author__name')
Enables search bar; __ enables searching across related model fields
4
readonly_fields = ('created_at', 'updated_at', 'word_count_display')
Marks fields as read-only in edit form
5
fieldsets = (('Content', {'fields': ...}), ...)
Groups fields into collapsible admin sections
6
author_name.short_description = 'Author'
Sets human-readable column header text
7
author_name.admin_order_field = 'author__name'
Enables sorting by related field author__name
8
format_html(...)
Safely renders HTML in admin without XSS vulnerabilities