Hello! Welcome back to your backend engineering course.
In our last few lessons, we've laid the entire foundation for our application's data layer. You've learned to:
- Define the structure of your data with Django models.
- Translate those models into a database schema using migrations.
- Perform all CRUD operations programmatically using the Django ORM.
- Write powerful and efficient queries with the QuerySet API to filter, sort, and retrieve specific data.
Today, we'll see how Django leverages all these pieces to provide one of its most celebrated features: the Django Admin. The learning outcome for this lesson is to be able to interact with and manage model data using the Django Admin interface.
This isn't just a tool; it's a fully functional web application built for you, for free. It reads the metadata from your models and automatically generates a complete, production-ready user interface for site administrators to manage content. For a backend developer, this is an incredible productivity booster, saving you from the tedious work of building internal data-management dashboards.
1. Activating the Admin and Gaining Access
By default, every new Django project includes the admin application and its URL configuration. Your first steps are to create a privileged user account that can access it and then explore the interface.
Creating a Superuser
To log into the admin, you need a user account with staff status. The simplest way to create one is with the createsuperuser management command.
Let's watch a practical demonstration from Dave Gray on how to start the development server, navigate to the admin URL, and create the necessary superuser account.
Watch from the beginning until 05:49. Pay attention to: The URL used to access the admin site (/admin/). The command python manage.py createsuperuser and the prompts it gives you. The default sections available after you log in for the first time (Users and Groups).
A Note on Security: Password Hashing
When you set your password, you might have noticed that Django enforces certain complexity rules. This is the first step in ensuring good security. More importantly, Django does not store your password in plain text. Instead, it stores a hash of the password.
This is a critical security principle. If a database were ever compromised, attackers would see a long, scrambled string of characters, not the actual passwords. This prevents them from being able to use those passwords on other systems. Given your interest in the "under the hood" aspects of systems, understanding this detail is important.
Django ORM Deep Dive - Models, Migrations and Admin UI Integration
This short clip from BugBytes provides an excellent explanation of why password hashing is so important and shows you what the hashed value looks like in the database.
Watch from 34:12 to 34:56. The key takeaway is how Django abstracts this crucial security practice away from you, handling it automatically when you use its authentication system.
2. Making Your Models Available in the Admin
After logging in, you probably noticed that the models you've created aren't visible anywhere. Django requires you to explicitly "register" the models you want to manage through the admin interface.
This is done in the admin.py file within each of your Django apps. The process is straightforward: import your model and pass it to admin.site.register().
Writing your first Django app, part 2
The official Django tutorial provides a clear, step-by-step guide to this process. Reading the docs is a fundamental skill for any developer.
Read the sections 'Make the poll app modifiable in the admin' and 'Explore the free admin functionality'. This will show you the single line of code needed to register a model and then walk you through the auto-generated interface for adding, changing, and deleting objects.
Once a model is registered, you gain a complete CRUD interface for it.


Now, let's see this in action.
Let's return to Dave Gray's video to watch him register a Post model and then use the admin to add, update, and view posts.
Watch from 05:49 to 08:46. Observe how a simple change in admin.py immediately results in a new, fully functional section within the admin web interface.
Test your understanding!
Take a model you created in a previous lesson.
- Open the
admin.pyfile in its corresponding app. - Import the model.
- Register it using
admin.site.register(). - Run your development server, log into the admin, and verify that you can now see your model.
- Use the interface to add a few new objects to your database.
This simple exercise solidifies the connection between your code and the auto-generated UI.
3. Customizing the Admin for Better Usability
The default admin is functional, but it can be significantly improved. Django provides a rich set of options for customizing how your models are displayed and behave in the admin. This is done by creating a ModelAdmin class and passing it to the register() function alongside your model.
This is where we can connect the querying concepts from our last lesson to a user interface!
Customizing the Change List Page
The "change list" is the page that shows all objects for a model. By default, it just shows the string representation (__str__) of each object. We can make this much more informative.
list_display: A tuple or list of field names to display as columns on the change list page.list_filter: Adds a sidebar that lets users filter the results based on the fields you specify. Django automatically provides sensible filter options based on the field type (e.g., date ranges for aDateTimeField).search_fields: Adds a search box that performs aLIKEquery on the database across the fields you specify.
Customizing the Edit Form
The form for adding or editing an object can also be tailored.
fieldsets: Lets you group related fields together under headings, breaking up large forms into logical sections.inlines: This is an incredibly powerful feature for managing related objects. If you have aForeignKeyrelationship, you can use an inline to edit the "child" objects directly on the "parent" object's edit page.
The next resource is a comprehensive guide to all these customizations.
Writing your first Django app, part 7
Part 7 of the Django tutorial is dedicated entirely to customizing the admin. It's a goldmine of practical techniques for building a more powerful and user-friendly admin interface.
Read the following three major sections: 'Customize the admin form': Focus on how fieldsets can be used to organize your edit pages. 'Adding related objects': This is crucial. Understand how to use StackedInline or TabularInline to manage related models efficiently. 'Customize the admin change list': See how list_display, list_filter, and search_fields give your users the power to find the data they need, directly applying the filtering concepts you learned previously.
Let's look at a quick example applying these concepts. If we have Question and Choice models:
# polls/admin.py
from django.contrib import admin
from .models import Question, Choice
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3 # Provides 3 extra blank slots for new choices
class QuestionAdmin(admin.ModelAdmin):
# Organizes the edit form
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
# Adds the ability to edit Choices on the Question page
inlines = [ChoiceInline]
# Customizes the list view
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']
# Register the Question model with its custom admin class
admin.site.register(Question, QuestionAdmin)
This single block of code transforms a basic admin page into a highly efficient data management tool with organized forms, inline editing of related objects, powerful filtering, and search functionality.
Conclusion
You now have a complete, secure interface for managing your application's data. You have not only learned how to enable the Django Admin but also how to customize it to create a user-friendly experience for administrators. This tool bridges the gap between your abstract data models and concrete, hands-on data management.
Key Takeaways:
- The Django Admin is an auto-generated CRUD interface for your models.
- Access requires a superuser created with
python manage.py createsuperuser. - Passwords are automatically and securely hashed.
- Models must be explicitly registered in
admin.pyusingadmin.site.register(). - The admin's functionality can be deeply customized using
ModelAdminclasses, allowing you to control list views (list_display,list_filter), search behavior (search_fields), form layouts (fieldsets), and the management of related objects (inlines).
Preview of the Next Module:
This lesson marks the end of our module on Database Modeling with the Django ORM. You are now fully equipped to define, migrate, and manage your application's data layer.
In the next module, we will shift our focus from internal data management to external data exposure. We'll begin our journey into building APIs with the Django REST Framework (DRF). Our very first lesson will cover the fundamental REST architectural principles and how to design resource-based APIs, setting the stage for serving your data to front-end frameworks, mobile apps, or any other client.