
Chapter Outline
Chapter 10: Django Admin — Leveraging Django’s Built-In Management Interface
In this chapter, you will learn:
- What the Django Admin is
- How to enable access to the Django Admin
- Registering models with the admin
- Customizing the Admin Views
- Permissions and Access Control
10.1 What is the Django Admin
Django Admin is one of Django’s most powerful built-in features. It provides a fully functional, configurable, database-driven web interface for managing your application’s data. With almost no setup, you can inspect, create, edit, delete, search, sort, and filter model instances — all with robust authentication and permissions already integrated.
While Django Admin is not meant to be your public-facing UI, it plays a crucial role in internal dashboards, editorial workflows, customer-support tools, and development-time debugging. In this chapter, you’ll learn how to register models with the admin site, customize admin pages, add search and list filters, connect Django Admin to forms, and enforce permissions. You’ll also explore best practices for using Django Admin in real production systems, where misconfiguration can affect both security and performance.
10.2 Enabling and Accessing Django Admin
Django Admin works out of the box, assuming you have the following:
django.contrib.admininINSTALLED_APPS- Authentication and session middleware enabled
- URL patterns mapped to the admin site
Your project already includes these settings. The final step is ensuring your urls.py exposes the admin:
src/blogsite/urls.py1from django.contrib import admin2from django.urls import path, include34urlpatterns = [5 path("admin/", admin.site.urls), # admin dashboard6 path("", include("apps.blog.urls")),7]
Next, enter the following command and follow the prompts to create a superuser:
bashpoetry run python manage.py createsuperuser
Then run the development server:
bashpoetry run python manage.py runserver
Navigate to: http://localhost:8000/admin/
Log in using your superuser credentials. You’ll see a pre-built interface for managing authentication data, permissions, and content types.
10.3 Registering Models with the Admin Site
By default, Django Admin only shows models you explicitly register.
10.3.1 Blog Post Model
Let's register the blog post model that we'd created in the previous chapter.
src/apps/blog/models.py1from django.db import models23class Post(models.Model):4 """Database model for blog posts."""56 title = models.CharField(max_length=100)7 content = models.TextField()8 created_at = models.DateTimeField(auto_now_add=True)910 def __str__(self) -> str:11 """Return a human-readable representation."""12 return f"{self.title} ({self.created_at:%Y-%m-%d})"
To display this in the admin, register it:
src/apps/blog/admin.py1from django.contrib import admin2from .models import Post34admin.site.register(Post)
Refresh the admin UI — “Posts” will appear in the Site adminstration page.
This allows you to inspect, and modify the data tied to this model.
10.4 Customizing Admin Views
The default view of the model uses the __str__() method to display a row of data on a table as shown below:
This is not terribly useful, since each record is represented by a single line of text.
However, the basic model registration is only the beginning. Django allows deep customization. We are going to customize this view, so that we see more fields of the model as columns on the table.
10.4.1 Adding List Display Options
Let's customize the model registration for Post, by extending the ModelAdmin object. The ModelAdmin class is the representation of your model in the admin interface.
The ModelAdmin is very flexible. It has several options for dealing with customizing the interface. Below we are using only a handful of these options:
src/apps/blog/admin.py1@admin.register(Post)2class PostAdmin(admin.ModelAdmin):3 list_display = ("id", "title", "created_at")4 search_fields = ("title",)5 list_filter = ("created_at",)
list_display: used to control which fields are displayed on the change list page of the admin.search_fields: used to enable search box on the admin change list.list_filter: used to activate filters in the right sidebar of the change list page.
After reloading, you should see the following:
This adds:
- A better column layout
- A search bar
- Sidebar filters
10.4.2 Organizing Form Layout (Fieldsets)
Set fielsets to control the layout of admin "add" and "chnage" pages.
fieldset is a list of 2-touples, in the format (name, field_options),
src/apps/blog/admin.py1fieldsets = (2 ("Post Information", {3 "fields": ("title", "content")4 }),5)
10.5 Using Custom Forms in Admin
Django Admin can use Django forms for consistent validation.
src/apps/blog/forms.py1from django import forms2from .models import Post34class PostAdminForm(forms.ModelForm):5 class Meta:6 model = Post7 fields = ["title", "content"]89 def clean_title(self):10 title = self.cleaned_data["title"]11 if len(title) < 5:12 raise forms.ValidationError("Title must be at least 5 characters long.")13 return title
Attach the form to your admin:
src/apps/blog/admin.py1from .forms import PostAdminForm23class PostAdmin(admin.ModelAdmin):4 form = PostAdminForm
You can test the form validation by entering an invalid title, and save it:
10.6 Permissions and Access Control
Django Admin uses Django's built-in permissions system.
Each model automatically gets:
add_postchange_postdelete_postview_post
You can add custom permissions:
src/apps/blog/models.py1class Post(models.Model):2 ...3 class Meta:4 permissions = [5 ("can_publish", "Can publish posts"),6 ]
The new custom permissions added to a model need to be migrated to the database. Therefore you'd need to run the following in the terminal:
bashpoetry run python manage.py makemigrations blogpoetry run python manage.py migrate
Then assign these to users or groups via the admin interface. This new permission would be available under user permissions:
10.7 How Django Admin Works Internally
The admin uses introspection and metadata from your models to build:
- Forms
- Tables
- Filters
- Search queries
- Relationships
This makes the admin extremely powerful for internal data management, but also means:
Never expose Django Admin to untrusted users.
Always:
- Restrict access
- Use strong passwords
- Enable HTTPS in production
10.8 Chapter Assignment
Build an Editorial Dashboard
- Create a model
Authorwith fields:name,bio,twitter_handle. - Create a model
Articlewith fields:title,body,author,status. - Register both models in the admin.
- Customize:
list_displaysearch_fieldslist_filterreadonly_fields
- Add a custom permission:
can_publish.
Bonus:
- Add a custom admin action
mark_as_published.
10.9 Chapter Summary
In this chapter we examined the basics of Django Admin. However there are many more additional features of Django admin including but not limited to security.
In this section we have reviewed some of the most important aspects of the Django framework. However Django is a fairly large Python framework, and should be studied comprehensively if you would like to write production grade applications using Django using more robust databases such as PostgreSQL, or MySQL; caching using Redis; Auth using OAuth 2, etc.
In the next section we will delve deeper into databases and data modeling in Python.
10.10 Further Reading
- Django Admin Docs: https://docs.djangoproject.com/en/stable/ref/contrib/admin/
- Django Permissions: https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions-and-authorization
Check your understanding
Test your knowledge of Django Admin