The Django admin

Django · Essentials · 12 min

The admin gives you a production-quality back office for your data with a few lines of code.

The admin (django.contrib.admin) builds a full CRUD interface from your models. Create an account with python manage.py createsuperuser, then visit /admin/.

Register models in the app's admin.py. A plain admin.site.register(Post) works; a ModelAdmin class customizes it:

  • list_display: columns on the change list, including methods and related fields
  • list_filter, search_fields, date_hierarchy: sidebar filters, a search box, date drill-down
  • list_editable, ordering, list_per_page, list_select_related
  • prepopulated_fields = {"slug": ["title"]}: fills the slug as you type
  • readonly_fields, fields or fieldsets: shape the edit form
  • autocomplete_fields or raw_id_fields: usable selects for foreign keys with many rows
  • inlines: edit related objects (comments) on the parent page with TabularInline or StackedInline
  • actions: functions applied to selected rows, such as "publish selected"

search_fields = ["title", "author__name"] searches across relationships; prefixes like "^title" or "=slug" change the matching.

Access is controlled by Django's permissions: staff users see only the models they have view, add, change or delete permission for.

The admin is meant for trusted staff, not as your public site's interface. When you need complex workflows for end users, build normal views.

Example

# blog/admin.py
from django.contrib import admin
from .models import Comment, Post

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

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ["title", "author", "status", "views", "created"]
    list_filter = ["status", "created"]
    search_fields = ["title", "author__name"]
    prepopulated_fields = {"slug": ["title"]}
    autocomplete_fields = ["author"]          # AuthorAdmin needs search_fields
    list_select_related = ["author"]
    date_hierarchy = "created"
    inlines = [CommentInline]
    actions = ["publish"]

    @admin.action(description="Publish selected posts")
    def publish(self, request, queryset):
        updated = queryset.update(status=Post.Status.PUBLISHED)
        self.message_user(request, f"{updated} posts published.")

Remember

  • createsuperuser, then register models in admin.py
  • ModelAdmin options shape lists, search, filters and forms
  • Inlines edit related rows; actions process selections
  • The admin is for staff, not end users

Watch out for

  • Using autocomplete_fields without search_fields on the related model's admin, which fails the system check.

Check yourself

  1. Which ModelAdmin option lets staff edit a post's comments on the post's page?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.djangoproject.com/en/6.0/ref/contrib/admin/

Builds on

Unlocks

Nothing builds on this yet.