Customizing the Django Admin

🐍 DjangoLesson 7Intermediate

One of Django's most powerful features is the automatic admin interface. It reads metadata from your models to provide a ready-to-use interface for managing content.

1 Customizing ModelAdmin
Python — admin.py
from django.contrib import admin
from .models import Post, Category

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ("title", "author", "category", "is_published", "created_at")
    list_filter = ("is_published", "created_at", "category")
    search_fields = ("title", "content")
    prepopulated_fields = {"slug": ("title",)}   # Auto slug generation
    raw_id_fields = ("author",)                  # Speed up loading for large database sets
    date_hierarchy = "created_at"

admin.site.register(Category)
2 Superuser Creation
Shell — Terminal Command
python manage.py createsuperuser
3 Code Challenge
Challenge: Add a custom admin action to the PostAdmin list view that allows admins to publish selected posts simultaneously using an admin action method.