Django Admin Basics: Your Secret Control Room 🎮
Imagine you built an amazing toy store. You have toys, customers, and sales happening every day. But how do you manage everything? You need a control room — a special place where you can see all your toys, add new ones, change prices, and keep track of everything.
Django Admin is exactly that — your secret control room for your website!
The Story: Building Your Toy Store Control Room
Let’s say you’re building a website for a toy store called “Happy Toys.” You have:
- Toys (name, price, description)
- Categories (action figures, puzzles, dolls)
- Orders from customers
Without Django Admin, you’d have to write code every time you want to add a new toy or change a price. That’s like having to rebuild part of your store every time you get new toys!
Django Admin gives you a beautiful, ready-made control room where you can do all this with just clicks.
1. Django Admin Overview
What Is It?
Django Admin is like a magic dashboard that Django builds for you automatically. It lets you:
- See all your data (toys, users, orders)
- Add new items
- Edit existing items
- Delete items you don’t need
The Magic Part
You write a few lines of code, and Django creates a whole management website for you!
# This is all you need to start!
from django.contrib import admin
from .models import Toy
admin.site.register(Toy)
Real-Life Example: When you log into Instagram’s admin panel, someone at Instagram can see all posts, all users, and manage everything. Django Admin does the same for YOUR website!
graph TD A["Your Website"] --> B["Django Admin"] B --> C["See All Data"] B --> D["Add New Items"] B --> E["Edit Items"] B --> F["Delete Items"] style B fill:#4ECDC4,stroke:#333
2. Creating a Superuser
The Master Key
A superuser is like having the master key to your control room. Only people with this key can enter and manage everything.
How to Create One
Open your terminal and type:
python manage.py createsuperuser
Django will ask you three things:
- Username — Your login name (like “admin”)
- Email — Your email address
- Password — Your secret password
Example Session:
Username: toystore_boss
Email: boss@happytoys.com
Password: ********
Password (again): ********
Superuser created successfully!
Now You Can Enter!
Go to http://localhost:8000/admin/ and log in with your username and password. Welcome to your control room!
graph TD A["Run createsuperuser"] --> B["Enter Username"] B --> C["Enter Email"] C --> D["Enter Password"] D --> E["Superuser Created!"] E --> F["Login at /admin/"] style E fill:#4ECDC4,stroke:#333
3. Registering Models in Admin
Making Your Toys Visible
When you create a Toy model, Django Admin doesn’t automatically know about it. You need to register it — like putting your toys on display in the control room.
Step 1: Create Your Model
# models.py
from django.db import models
class Toy(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(
max_digits=10,
decimal_places=2
)
description = models.TextField()
in_stock = models.BooleanField(default=True)
Step 2: Register It
# admin.py
from django.contrib import admin
from .models import Toy
admin.site.register(Toy)
What Happens?
Now when you go to /admin/, you’ll see “Toys” in your control room. You can add, edit, and delete toys!
Register Multiple Models
# admin.py
from django.contrib import admin
from .models import Toy, Category, Order
admin.site.register(Toy)
admin.site.register(Category)
admin.site.register(Order)
Now you see all three in your admin panel!
4. ModelAdmin Class: Customizing Your Control Room
Going Beyond Basic
The basic registration works, but what if you want your control room to be prettier and smarter?
The ModelAdmin class lets you customize everything!
Basic Structure
# admin.py
from django.contrib import admin
from .models import Toy
class ToyAdmin(admin.ModelAdmin):
# Your customizations go here
pass
admin.site.register(Toy, ToyAdmin)
Or Use a Decorator (Cleaner!)
from django.contrib import admin
from .models import Toy
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
pass
Both do the same thing — the decorator is just shorter and cleaner!
graph TD A["ModelAdmin Class"] --> B["List Options"] A --> C["Form Options"] A --> D["Search & Filter"] A --> E["Custom Actions"] style A fill:#FF6B6B,stroke:#333
5. Admin List Options: Making Your List Beautiful
When you see all your toys in the admin, you see a list. Let’s make this list super useful!
list_display — What Columns to Show
By default, you only see the toy name. But you want to see price and stock too!
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = [
'name',
'price',
'in_stock'
]
Now your list shows three columns: Name, Price, and In Stock!
list_filter — Add Filter Boxes
Want to quickly see only toys that are in stock?
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'in_stock']
list_filter = ['in_stock', 'category']
A sidebar appears with clickable filters!
search_fields — Add a Search Box
Looking for a specific toy?
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'in_stock']
list_filter = ['in_stock']
search_fields = ['name', 'description']
Now you can type “robot” and find all robot toys!
list_editable — Edit Right in the List
Change prices without opening each toy:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'in_stock']
list_editable = ['price', 'in_stock']
Now you can edit price and stock directly in the list!
ordering — Sort Your List
Show newest toys first:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'in_stock']
ordering = ['-price'] # Highest price first
The - means descending (highest to lowest).
Complete Example
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
list_display = [
'name',
'price',
'category',
'in_stock'
]
list_filter = ['in_stock', 'category']
search_fields = ['name', 'description']
list_editable = ['price', 'in_stock']
ordering = ['-price']
list_per_page = 20
6. Admin Form Options: Customizing the Edit Page
When you click on a toy to edit it, you see a form. Let’s make this form amazing!
fields — Choose Which Fields to Show
Maybe you don’t want people to edit certain fields:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
fields = ['name', 'price', 'description']
# 'in_stock' won't show in the form
exclude — Hide Specific Fields
The opposite of fields:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
exclude = ['created_at']
# Everything shows EXCEPT created_at
fieldsets — Group Fields Nicely
Make your form organized with sections:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
fieldsets = [
('Basic Info', {
'fields': ['name', 'description']
}),
('Pricing', {
'fields': ['price', 'discount_price']
}),
('Inventory', {
'fields': ['in_stock', 'quantity'],
'classes': ['collapse']
}),
]
Now your form has three neat sections! The collapse class makes that section collapsible.
readonly_fields — View-Only Fields
Some fields shouldn’t be changed:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
readonly_fields = ['created_at', 'updated_at']
Users can see these dates but can’t change them!
prepopulated_fields — Auto-Fill Magic
Perfect for creating URL slugs:
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
prepopulated_fields = {
'slug': ['name']
}
When you type “Super Robot”, the slug automatically becomes “super-robot”!
Complete Form Example
@admin.register(Toy)
class ToyAdmin(admin.ModelAdmin):
# List options
list_display = ['name', 'price', 'in_stock']
list_filter = ['in_stock', 'category']
search_fields = ['name']
# Form options
fieldsets = [
('Basic Info', {
'fields': ['name', 'slug', 'description']
}),
('Pricing & Stock', {
'fields': ['price', 'in_stock', 'quantity']
}),
]
prepopulated_fields = {'slug': ['name']}
readonly_fields = ['created_at']
The Complete Picture
graph TD A["Django Admin"] --> B["Create Superuser"] A --> C["Register Models"] C --> D["ModelAdmin Class"] D --> E["List Options"] D --> F["Form Options"] E --> G["list_display"] E --> H["list_filter"] E --> I["search_fields"] E --> J["list_editable"] F --> K["fields/exclude"] F --> L["fieldsets"] F --> M["readonly_fields"] style A fill:#667eea,stroke:#333,color:#fff style D fill:#4ECDC4,stroke:#333
Quick Summary
| What | Why | How |
|---|---|---|
| Admin Overview | Manage your data easily | Built-in dashboard |
| Superuser | Get access to admin | createsuperuser command |
| Register Models | Make models visible | admin.site.register() |
| ModelAdmin | Customize everything | Create a class |
| List Options | Better list view | list_display, list_filter, etc. |
| Form Options | Better edit forms | fieldsets, readonly_fields, etc. |
You Did It! 🎉
You now know how to:
- Understand what Django Admin is
- Create a superuser to access it
- Register your models so they appear
- Use ModelAdmin to customize everything
- Make your list views informative and easy to use
- Create organized, user-friendly edit forms
Django Admin is your superpower — it saves you hours of building management interfaces. Use it wisely, and your control room will be the envy of every developer!
Now go build something amazing! 🚀
