CodeOath
← All posts
Python70 min total · 18 parts

Django Fundamentals: MVT Architecture, the ORM, and Middleware

Contents — Part 13 of 18: The Admin Site
Part 13 of 18 · ~1 min

The Admin Site

# admin.py
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ["id", "customer_name", "total", "status", "created_at"]
    list_filter = ["status"]
    search_fields = ["customer_name"]
    list_select_related = ["customer"]   # avoids N+1 queries on the admin list page too

Registering a model gives a full CRUD interface at /admin/ for free — genuinely one of Django's biggest productivity advantages for internal tools and back-office work, and worth knowing well because it's frequently the very first thing a new Django feature needs (a way for non-engineers to view/edit the data). list_select_related matters for exactly the same reason select_related does everywhere else — the admin's list view is just as susceptible to N+1 queries as any hand-written view.