Admin Panel in Django
What is Django Admin?
Django Admin is a built-in web-based interface provided by Django that allows you to manage your application's data easily. It lets you create, read, update, and delete (CRUD) records from your database through a user-friendly dashboard.
- We can access it by default at the `/admin/` URL.
- Only superusers or staff users with admin rights can access it.
- We can register models to edit them in the admin panel.
- We can customize admin models using ModelAdmin and UserAdmin.
from django.contrib import admin
from django.urls import path
urlpatterns = [
# Add the admin panel URL
path('admin/', admin.site.urls),
# Add other URLs as needed.
]
Customizing Django Admin Panel Text
Django Admin is a built-in web-based interface provided by Django that allows you to manage your application's data easily. It lets you create, read, update, and delete (CRUD) records from your database through a user-friendly dashboard.
- We can access it by default at the `/admin/` URL.
- Only superusers or staff users with admin rights can access it.
- We can register models to edit them in the admin panel.
- We can customize admin models using ModelAdmin and UserAdmin.
# admin.py
from django.contrib import admin
admin.site.site_header = "My Admin Panel"
admin.site.site_title = "Admin Login"
admin.site.index_title = "Welcome to the Admin Dashboard"
]
Customizing Admin with ModelAdmin
ModelAdmin in Django customizes a model's display and behavior in the admin panel. It provides options for list views, form layouts, filtering, and searching.
List Display: Customize which fields are shown in the list view.
Search Fields: Allow searching of specific fields in the admin.
Filters: Add filters to help narrow down the displayed data.
Form Layout: Control how forms are presented for adding/editing instances of the model.
Inline Editing: Add related models directly within the same page.
from django.contrib import admin
from .models import Post
# Registering model with custom ModelAdmin without using decorators
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'created_at')
search_fields = ('title',)
list_filter = ('created_at',)
# Registering the model with custom ModelAdmin
admin.site.register(Post, PostAdmin)
# Using Decorators to register model with custom ModelAdmin
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
# Customizing model display and behavior in admin
list_display = ('title', 'created_at')
search_fields = ('title',)
list_filter = ('created_at',)
Customizing Admin with UserAdmin
UserAdmin in Django allows you to customize how the User model is displayed and managed in the admin panel. It offers options for configuring how user details, permissions, and groups are presented and edited. It also allows creating users directly from the admin panel.
Fieldsets: Control the organization of fields in the user form.
add_fieldsets: Defines the fields shown on the user creation page in the admin.
List Display: Customize which fields appear in the user list view.
Search Fields: Enable search functionality on user-related fields.
Permissions: Manage user permissions and groups directly in the admin panel.
Create User: You can create new users from the admin interface with the customized settings.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
# from django.contrib.auth.models import User
# Importing the custom user model
from django.contrib.auth import get_user_model
MyUser = get_user_model()
class CustomUserAdmin(UserAdmin):
list_display = ('username', 'email', 'first_name', 'role', 'is_staff')
search_fields = ('username', 'email')
# Group fields into sections on the user edit page in admin
fieldsets = (
(None, {'fields': ('username', 'password', 'role')}),
('Personal info', {'fields': ('first_name', 'last_name', 'email')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser')}),
)
# Fields shown on the first page when adding a new user
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2', 'role'),
}),
)
# admin.site.register(User, CustomUserAdmin)
admin.site.register(MyUser, CustomUserAdmin)
# Registering MyUser model with CustomUserAdmin using decorator
@admin.register(MyUser)
class CustomUserAdmin(UserAdmin):
pass
Using Inline Models in Django Admin
Inline refers to the feature that allows managing related models directly within the parent model's
form. It is
particularly useful for models with
Types of Inline Models:
- TabularInline
- StackedInline
1. TabularInline:
TabularInline displays related objects in a compact, table-like layout in the Django Admin. It is useful when you want a more concise and organized view of related models.
from django.contrib import admin
from .models import Post, Comment
# TabularInline class for related 'Comment' model
class CommentInline(admin.TabularInline):
model = Comment
extra = 1 # Number of empty forms to display
# Registering 'Post' model with inline 'Comment' model
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
inlines = [CommentInline] # Adding 'Comment' model inline in 'Post' admin
2. StackedInline:
StackedInline displays related objects in a detailed, stacked layout in the Django Admin. It is useful when you want to provide a clear, form-like view of related models, making it easy to edit multiple fields of the related model.
from django.contrib import admin
from .models import Post, Comment
# StackedInline class for related 'Comment' model
class CommentInline(admin.StackedInline):
model = Comment
extra = 1 # Number of empty forms to display
# Registering 'Post' model with inline 'Comment' model
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
inlines = [CommentInline] # Adding 'Comment' model inline in 'Post' admin
Exercise
- Fill in the Blanks:
- The __________ decorator registers a model with the Django admin.
- - To register models in the admin, use the __________ function.
- - The __________ class customizes model behavior in the Django Admin.
- - The __________ attribute defines the fields displayed in the list view of a model.
- - The __________ attribute customizes the layout of fields when adding a new model instance.