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 fieldslist_filter,search_fields,date_hierarchy: sidebar filters, a search box, date drill-downlist_editable,ordering,list_per_page,list_select_relatedprepopulated_fields = {"slug": ["title"]}: fills the slug as you typereadonly_fields,fieldsorfieldsets: shape the edit formautocomplete_fieldsorraw_id_fields: usable selects for foreign keys with many rowsinlines: edit related objects (comments) on the parent page withTabularInlineorStackedInlineactions: 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.