Django provides a powerful built-in admin interface. By registering models in admin.py, you can manage application data through a clean and secure UI without writing custom dashboards.
admin.site.register()ModelAdminThe admin.py file controls how models appear and behave in the Django Admin dashboard. Models must be explicitly registered to become visible.
# Import Django admin module
from django.contrib import admin
from .models import Product
# Register Product model to appear in admin panel
admin.site.register(Product)
To control how models are displayed, define a custom ModelAdmin class and pass it during registration.
# Import admin tools
from django.contrib import admin
from .models import Product
# Custom admin configuration for Product
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'stock')
search_fields = ('name',)
# Register model with custom admin options
admin.site.register(Product, ProductAdmin)
Toggle the checkboxes below to see how adding fields to list_display affects both the Python code and the Admin Dashboard view.
| ID | Name | Category | Price | Stock |
|---|---|---|---|---|
| 101 | Gaming Laptop | Electronics | $1200 | 15 |
| 102 | Wireless Mouse | Accessories | $45 | 50 |
Inline admin allows managing related models directly inside a parent model’s admin page.
# Import required modules
from django.contrib import admin
from .models import Order, Product
# Inline admin for products inside orders
class ProductInline(admin.TabularInline):
model = Order.products.through
extra = 1
# Order admin using inline products
class OrderAdmin(admin.ModelAdmin):
inlines = [ProductInline]
# Register Order with inline configuration
admin.site.register(Order, OrderAdmin)
Once registered, models appear in the Django Admin sidebar. Custom fields, search boxes, and inline forms become instantly available without extra UI coding.
list_display for better readabilitysearch_fields for large datasetsadmin.py