The goal of this tutorial is to guide you through the process of creating a custom admin interface in Django. By the end of this tutorial, you'll have a firm understanding of how to customize Django's built-in admin interface to fit your project needs.
What you will learn:
1. Using and customizing Django's built-in admin interface
2. Creating custom admin actions
3. Customizing the admin list page
4. Creating custom admin forms
Prerequisites:
Django's admin interface is a powerful feature that can be customized to a great extent. This guide will walk you through the process step by step.
Firstly, ensure that 'django.contrib.admin' is included in your INSTALLED_APPS
setting:
INSTALLED_APPS = [
#...
'django.contrib.admin',
#...
]
Register your models with the admin interface. In your app, create an admin.py
file (if it doesn't exist) and register your model:
from django.contrib import admin
from .models import YourModel
admin.site.register(YourModel)
You can customize the admin interface by creating an admin class that inherits from admin.ModelAdmin
:
class YourModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
You can decide which fields to display on the change list page of the admin interface:
class YourModelAdmin(admin.ModelAdmin):
list_display = ('field1', 'field2',)
admin.site.register(YourModel, YourModelAdmin)
You can add filters to the change list page of the admin interface:
class YourModelAdmin(admin.ModelAdmin):
list_filter = ('field1', 'field2',)
admin.site.register(YourModel, YourModelAdmin)
You can create custom actions to be performed on selected objects:
class YourModelAdmin(admin.ModelAdmin):
actions = ['custom_action']
def custom_action(self, request, queryset):
# Perform your action here
pass
custom_action.short_description = 'Custom Action'
admin.site.register(YourModel, YourModelAdmin)
We have covered how to set up and customize Django's admin interface, including how to create custom admin actions and how to customize the admin list page.
For further learning, you can look into Django's documentation on the admin site, which covers more advanced customization options.
Exercise 1: Create a custom admin interface for a blog post model. The list display should include the title, author, and date published. Add filters for the author and date published.
Solution:
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'date_published')
list_filter = ('author', 'date_published')
admin.site.register(Post, PostAdmin)
Exercise 2: Add a custom action to the post model's admin interface that allows you to mark selected posts as published.
Solution:
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'date_published')
list_filter = ('author', 'date_published')
actions = ['mark_published']
def mark_published(self, request, queryset):
queryset.update(status='p')
mark_published.short_description = 'Mark selected posts as Published'
admin.site.register(Post, PostAdmin)
Exercise 3: Create a custom admin form for the post model that includes validation.
Solution:
from django import forms
class PostAdminForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__'
def clean_title(self):
title = self.cleaned_data.get('title')
if not title:
raise forms.ValidationError('This field cannot be blank.')
return title
class PostAdmin(admin.ModelAdmin):
form = PostAdminForm
admin.site.register(Post, PostAdmin)
Happy coding!