📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Django Framework Django Admin

Django Admin

6 min read Quiz at the end
Customise the admin with list_display, search_fields, filters, inlines, and bulk actions.

Django Admin

from django.contrib import admin
from .models import Post, Tag

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display   = ("title", "author", "is_draft", "created_at")
    list_filter    = ("is_draft", "author", "tags")
    search_fields  = ("title", "body")
    prepopulated_fields = {"slug": ("title",)}
    date_hierarchy = "created_at"
    ordering       = ("-created_at",)
    readonly_fields = ("created_at", "updated_at")
    filter_horizontal = ("tags",)

    # Custom action
    @admin.action(description="Publish selected posts")
    def publish(self, request, queryset):
        queryset.update(is_draft=False)
    actions = ["publish"]

    # Inline editor
    class CommentInline(admin.TabularInline):
        model = Comment
        extra = 0

    inlines = [CommentInline]

@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ("name", "post_count")

    def post_count(self, obj):
        return obj.posts.count()
Topic Quiz · 2 questions

Test your understanding before moving on

1. What does @admin.register() do?
💡 @admin.register(Post) associates PostAdmin with Post in the admin.
2. What does list_display control in ModelAdmin?
💡 list_display is a tuple of field names shown as columns in admin list view.