Django Admin Configuration Options

Django, the high-level web framework written in Python, has made it easy for developers to build robust web applications quickly.
One of Django’s most powerful features is its built-in admin interface, which allows developers and site administrators to manage application data without having to build custom CRUD (Create, Read, Update, Delete) interfaces.
This article will delve deep into the configuration options for Django Admin.
1. Basic Setup:
Before diving into configuration options, ensure you’ve included 'django.contrib.admin' and 'django.contrib.auth' in the INSTALLED_APPS settings of your project.
Also, you should wire up the admin URL in your project’s URL configuration.
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]2. ModelAdmin Options:
Once your models are created, you can register them with the admin interface. The ModelAdmin class provides numerous options to customize their representation:
- list_display: Define which fields of the model should be displayed on the object list page.
- list_filter: Adds filter sidebar for fields specified.
- search_fields: Provide a search bar allowing users to search model fields.
- ordering: Specify the default sorting order.
- date_hierarchy: Adds date-based drill-down navigation.
- list_editable: Allows in-place editing of specified fields directly from the list view.
- exclude: Fields to be excluded from the form on the change page.
- fields: Control the layout of the admin form.
- inlines: Used for editing related models inline.
Let’s illustrate some of the ModelAdmin options using an example model Book:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
published_date = models.DateField()
is_published = models.BooleanField(default=True)
genre = models.CharField(max_length=50, choices=[
('fiction', 'Fiction'),
('non-fiction', 'Non-fiction'),
('biography', 'Biography'),
('children', 'Children'),
])For this model, we’ll now create a custom BookAdmin class with various configuration options and register it with the admin site.
To show specific fields in the object list page:
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date', 'is_published')To add a filter sidebar:
class BookAdmin(admin.ModelAdmin):
list_filter = ('is_published', 'genre', 'published_date')To search specific fields:
class BookAdmin(admin.ModelAdmin):
search_fields = ['title', 'author']Default ordering:
class BookAdmin(admin.ModelAdmin):
ordering = ['-published_date']Date-based drill-down navigation:
class BookAdmin(admin.ModelAdmin):
date_hierarchy = 'published_date'In-place editing:
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date', 'is_published')
list_editable = ['is_published']Note: Fields in list_editable must also be in list_display.
Excluding fields from the form:
class BookAdmin(admin.ModelAdmin):
exclude = ('published_date',)To customize the layout:
class BookAdmin(admin.ModelAdmin):
fields = ('title', 'author', ('genre', 'is_published'), 'published_date')Finally, register the model with its custom admin configuration:
admin.site.register(Book, BookAdmin)
These are just a few of the various options available in ModelAdmin.
By integrating these options, you can customize the Django admin interface according to the needs of your application, streamlining the management of your data.
3. Admin Site Configuration:
The AdminSite class provides various attributes and methods to customize the look and feel of the admin site at a higher level:
- site_header: Customize the text that appears at the top of the admin index page.
- site_title: Set the title text for admin pages.
- index_title: Title for the admin site’s home page.
- site_url: URL for the “View site” link at the top of each admin page.
- empty_value_display: Set a custom display string for empty fields.
- login_template, index_template, etc.: Customize the template used for respective admin views.
Here’s how you can set these options:
Customize the text at the top of the admin index page.
from django.contrib import admin
admin.site.site_header = "My Custom Admin Portal"Change the title text for admin pages.
admin.site.site_title = "My Site Admin"Title for the admin site’s home page.
admin.site.index_title = "Welcome to the Custom Admin Area"URL for the “View site” link at the top of each admin page. Set it to None to remove the link.
admin.site.site_url = "/my-custom-site-url/"Set a custom display string for fields that are empty.
admin.site.empty_value_display = 'Not Provided'Customize the template used for respective admin views.
admin.site.login_template = 'my_custom_template/login.html'By understanding and leveraging these configurations, you can enhance the backend experience for yourself and other administrators.
4. Admin Media Customization:
While Django’s admin provides a visually appealing interface, you might want to style it according to your brand.
You can override the default CSS and JavaScript by creating a custom template.
Customizing the media (CSS, JavaScript, etc.) of the Django admin site can greatly enhance its appearance and usability.
The Django admin interface uses its own set of media files.
However, sometimes you might want to override these or add your own to provide a unique look and feel or add additional functionalities.
Override CSS:
If you want to change the appearance of the Django admin, one common approach is to override its CSS.
a) Create a new CSS file, say custom_admin.css, and place it in a directory within one of your app's static directories. For instance, myapp/static/myapp/css/custom_admin.css.
b) Inside the ModelAdmin class or the AdminSite class, override the Media class:
from django.contrib import admin
class CustomAdminMedia(admin.ModelAdmin):
class Media:
css = {
'all': ('myapp/css/custom_admin.css',)
}
admin.site.register(MyModel, CustomAdminMedia)Override JavaScript:
If you want to add new functionalities or change existing ones, you can add or override JavaScript.
a) Create a new JS file, say custom_admin.js, and place it in a directory like myapp/static/myapp/js/custom_admin.js.
b) Override the Media class in a similar manner:
class CustomAdminJS(admin.ModelAdmin):
class Media:
js = ('myapp/js/custom_admin.js',)
admin.site.register(MyModel, CustomAdminJS)Combining CSS & JS:
You can combine both CSS and JS in the Media class.
class CustomAdminMediaJS(admin.ModelAdmin):
class Media:
css = {
'all': ('myapp/css/custom_admin.css',)
}
js = ('myapp/js/custom_admin.js',)
admin.site.register(MyModel, CustomAdminMediaJS)Admin Templates Customization:
Beyond CSS and JS, you can also customize the templates that the Django admin uses. For instance, you can override admin/base_site.html to make broad changes to the admin site's layout.
To do this:
a) Within your project’s templates directory, create a directory named admin.
b) Inside the admin directory, mimic the path to the template you wish to override. For example, to override base_site.html, the path would be templates/admin/base_site.html.
c) Now modify this template. For instance, to change the title:
{% extends "admin/base.html" %}
{% load i18n static %}
{% block title %} My Custom Admin {% endblock %}Remember, when overriding templates, always extend from the original template (as shown above) to ensure you maintain all the necessary blocks and content.
By customizing media and templates, you can make the Django admin interface align more closely with the branding and functionality needs of your project.
5. Admin Actions:
Admin actions allow performing batch operations on selected items from the list view.
By default, Django provides a delete action, but you can write custom actions tailored to your needs.
They can be a powerful tool to enhance the usability of your admin site.
Let’s walk through some examples.
Marking Books as Published
Let’s use a Book model with an is_published field:
class Book(models.Model):
title = models.CharField(max_length=200)
is_published = models.BooleanField(default=False)You want an action to mark selected books as published:
def mark_as_published(modeladmin, request, queryset):
queryset.update(is_published=True)
mark_as_published.short_description = "Mark selected books as published"Then, in your ModelAdmin, add the action:
class BookAdmin(admin.ModelAdmin):
list_display = ['title', 'is_published']
actions = [mark_as_published]
admin.site.register(Book, BookAdmin)Exporting Selected Items to CSV
This is useful if you want to download data from the admin interface:
import csv
from django.http import HttpResponse
def export_to_csv(modeladmin, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="books.csv"'
writer = csv.writer(response)
writer.writerow(['Title', 'Published'])
for book in queryset:
writer.writerow([book.title, book.is_published])
return response
export_to_csv.short_description = "Export selected books to CSV"Then, just like the previous example, add the action to ModelAdmin:
class BookAdmin(admin.ModelAdmin):
list_display = ['title', 'is_published']
actions = [mark_as_published, export_to_csv]
admin.site.register(Book, BookAdmin)Custom Confirmation Page for Admin Action
Sometimes, you might want to present a confirmation page before an action is performed:
from django.urls import path
from django.shortcuts import render
def delete_selected_books(modeladmin, request, queryset):
if 'apply' in request.POST:
queryset.delete()
modeladmin.message_user(request, "Deleted selected books")
return
return render(request, 'admin/confirm_delete.html', {'books': queryset})
delete_selected_books.short_description = "Delete selected books with confirmation"
# Inside the ModelAdmin class:
class BookAdmin(admin.ModelAdmin):
list_display = ['title', 'is_published']
actions = [mark_as_published, export_to_csv, delete_selected_books]
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('delete_selected/', self.admin_site.admin_view(delete_selected_books), name='delete_selected_books'),
]
return custom_urls + urlsIn this example, before books are deleted, the admin user will be directed to a confirmation page (confirm_delete.html).
This allows for an extra layer of validation before performing destructive actions.
6. Advanced Features:
- form: Override the default form used for the edit page.
- filter_horizontal and filter_vertical: Widgets to manage many-to-many relationships.
- readonly_fields: Fields that should be displayed as read-only.
- save_as: Allows adding objects via duplication.
- save_on_top: Show save buttons at both the top and bottom of the change form.
Let’s dive into each of these advanced features:
form — Override the default form used for the edit page.
You can define a custom form for the admin page of a model.
This can be useful if you want to modify field ordering, add custom validation, or adjust widgets.
from django import forms
from .models import Book
class BookAdminForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
widgets = {
'title': forms.TextInput(attrs={'class': 'custom-class'}),
}
class BookAdmin(admin.ModelAdmin):
form = BookAdminForm
admin.site.register(Book, BookAdmin)filter_horizontal and filter_vertical — Widgets to manage many-to-many relationships.
These widgets are designed to make it more user-friendly to manage many-to-many relationships.
Assuming a model Author and modifying our Book model to have a many-to-many relationship:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
authors = models.ManyToManyField(Author)In the ModelAdmin:
class BookAdmin(admin.ModelAdmin):
filter_horizontal = ('authors',)
admin.site.register(Book, BookAdmin)Use filter_vertical instead of filter_horizontal if you prefer a vertical layout.
readonly_fields -Fields that should be displayed as read-only.
This ensures some fields can be displayed but not edited.
class BookAdmin(admin.ModelAdmin):
readonly_fields = ('title',)
admin.site.register(Book, BookAdmin)save_as — Allows adding objects via duplication.
When set to True, this provides a "Save as new" button on the change form, allowing admins to save a duplicate of the existing object.
class BookAdmin(admin.ModelAdmin):
save_as = True
admin.site.register(Book, BookAdmin)save_on_top — Show save buttons at both the top and bottom of the change form.
This can be useful for long forms:
class BookAdmin(admin.ModelAdmin):
save_on_top = True
admin.site.register(Book, BookAdmin)By leveraging these advanced features of Django’s admin interface, you can tailor the admin backend to be more user-friendly and efficient for your project’s specific needs.
Conclusion:
Django Admin’s configuration options are a testament to its flexibility and adaptability.
Whether you’re looking to make simple tweaks or overhaul the entire interface, Django provides you with the tools to make it happen.
Properly utilized, the Django Admin interface can save developers countless hours, allowing them to focus on building unique features and improving their applications.
Thank you for reading and I will see you on the Internet.
Follow me on Twitter: https://twitter.com/DevAsService
Check out my website at: https://developer-service.io/
Check out other articles that might interest you:
If you want to stay updated when I post a new article, you can signup for my free newsletter!





