Full answer
The admin (django.contrib.admin) reads your models and builds list, add, edit and delete pages for staff users. It needs auth, contenttypes, sessions and messages in INSTALLED_APPS and a URL such as path("admin/", admin.site.urls). Create a login with python manage.py createsuperuser.
Registering with customisation:
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ["title", "author", "copies", "is_available"]
list_filter = ["author"]
search_fields = ["title", "author__name"]Useful options:
list_displaypicks columns. It can include model methods or ModelAdmin methods, which you can label with the@admin.display(description=..., boolean=True)decorator.list_filteradds a sidebar filter;search_fieldsadds a search box (author__namesearches across a relation).ordering,list_per_page,date_hierarchy.readonly_fields,fieldsorfieldsetsshape the edit form.inlinesedit child rows (for exampleTabularInlinefor book copies) on the parent's page.list_select_relatedorget_queryset()withselect_relatedavoids one query per row when a column shows a foreign key.actionsadd bulk operations, like "mark selected books as lost".
Permissions come from the auth app: each model gets add, change, delete and view permissions, and staff users see only what they are allowed to.
The admin is an internal tool. Customer-facing pages should be normal views, because the admin's layout, URLs and permission model are built for staff.
import sys
import types
import django
from django.conf import settings
# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__
settings.configure(
SECRET_KEY="demo-only-key-not-for-production",
ALLOWED_HOSTS=["testserver"], # the host name used by Django's test client
INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "django.contrib.admin",
"django.contrib.sessions", "django.contrib.messages", "school"],
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()
from django.contrib import admin
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=80)
author = models.CharField(max_length=60)
copies = models.PositiveIntegerField(default=1)
class Meta:
app_label = "school"
def __str__(self):
return self.title
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ["title", "author", "copies", "is_available"]
list_filter = ["author"]
search_fields = ["title", "author"]
ordering = ["title"]
@admin.display(boolean=True, description="Available?")
def is_available(self, obj):
return obj.copies > 0
print(admin.site.is_registered(Book))
model_admin = admin.site._registry[Book]
print(model_admin.list_display, model_admin.search_fields)
print(model_admin.is_available(Book(title="Godan", author="Premchand", copies=0)))
Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. The snippet inspects the registered ModelAdmin instead of starting a server. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): True ['title', 'author', 'copies', 'is_available'] ['title', 'author'] False
Key points
- Register with @admin.register and a ModelAdmin
- list_display, list_filter, search_fields shape the list page
- Inlines edit related rows on the parent page
- Use list_select_related to avoid N+1 in the admin list
- Admin is for staff, not public users
Common mistakes
- Using the admin as the public user interface
- Showing a foreign key column without list_select_related and getting one query per row
- Giving every staff user superuser rights instead of using permissions
- Leaving the admin at /admin/ with no rate limiting or 2FA on a public site
Follow-ups
- How would you add a bulk action to the admin?
- How can you restrict which rows a staff user sees in the admin?
- How do inlines work for a one-to-many relation?
Tips
- Name four ModelAdmin options with what each does
- Mention the N+1 risk in list_display
- Say clearly that the admin is for internal staff