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()