Django Admin Third-Party
Django's built-in admin interface is incredibly powerful out of the box, but there's a vast ecosystem of third-party packages that can extend its functionality even further. In this guide, we'll explore some of the most useful third-party packages for Django admin that can save you time and improve your admin experience.
Introduction to Django Admin Third-Party Packages
The Django admin interface provides a convenient way to manage your application's data. However, as your projects grow in complexity, you might find yourself needing additional functionality beyond what the default admin offers. This is where third-party packages come in.
Third-party packages for Django admin can:
- Improve the visual appearance and user experience
- Add advanced filtering and searching capabilities
- Provide specialized widgets and field types
- Implement security enhancements
- Add dashboard and reporting features
Let's explore some of the most popular and useful packages you can integrate into your Django projects.
Django Admin Interface Themes
django-admin-interface
The django-admin-interface package provides a modern, responsive interface for the Django admin with customizable themes, logos, and more.
Installation
pip install django-admin-interface
Add it to your INSTALLED_APPS
in settings.py
:
INSTALLED_APPS = [
'admin_interface',
'colorfield',
'django.contrib.admin',
# other apps...
]
Remember to migrate your database after adding the package:
python manage.py migrate
With django-admin-interface, you can customize your admin interface through the admin panel itself, including colors, logo, title, and more.
django-jazzmin
Django-jazzmin provides a beautiful and responsive admin interface based on AdminLTE.
Installation
pip install django-jazzmin
Add it to your INSTALLED_APPS
before django.contrib.admin
:
INSTALLED_APPS = [
'jazzmin',
'django.contrib.admin',
# other apps...
]
You can customize jazzmin by adding a configuration dictionary in your settings.py
:
JAZZMIN_SETTINGS = {
"site_title": "My Django Site",
"site_header": "My Admin",
"site_brand": "My Site",
"site_logo": "books/img/logo.png",
"welcome_sign": "Welcome to the my site",
"copyright": "My Library Ltd",
"search_model": ["auth.User", "auth.Group"],
"user_avatar": None,
# Other settings...
}
Enhanced List Views
django-admin-list-filter-dropdown
This package replaces the traditional sidebar filters with dropdown filters, which can save screen space and make filtering more intuitive.
Installation
pip install django-admin-list-filter-dropdown
Usage example:
from django.contrib import admin
from admin_list_filter_dropdown.filters import RelatedDropdownFilter, ChoiceDropdownFilter
@admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
list_filter = (
('related_field', RelatedDropdownFilter),
('choice_field', ChoiceDropdownFilter),
)
django-admin-rangefilter
This package allows you to filter by date ranges in the admin list views.
Installation
pip install django-admin-rangefilter
Usage example:
from django.contrib import admin
from rangefilter.filters import DateRangeFilter
@admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
list_filter = (
('created_date', DateRangeFilter),
)
Enhanced Edit Views
django-admin-autocomplete-filter
This package brings autocomplete functionality to admin filters, which is especially useful for models with many related objects.
Installation
pip install django-admin-autocomplete-filter
Usage example:
from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilter
class AuthorFilter(AutocompleteFilter):
title = 'Author'
field_name = 'author'
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_filter = [AuthorFilter]
search_fields = ['author__name']
django-import-export
This popular package allows you to import and export data in various formats (CSV, Excel, JSON, etc.) from the admin interface.
Installation
pip install django-import-export
Add it to your INSTALLED_APPS
:
INSTALLED_APPS = [
# other apps...
'import_export',
]
Usage example:
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from .models import Book
class BookResource(resources.ModelResource):
class Meta:
model = Book
fields = ('id', 'title', 'author', 'published_date')
@admin.register(Book)
class BookAdmin(ImportExportModelAdmin):
resource_class = BookResource
Advanced Admin Extensions
django-admin-honeypot
This security package creates a fake admin login page to capture potential attackers while directing legitimate users to the actual login page.
Installation
pip install django-admin-honeypot
Add it to your INSTALLED_APPS
:
INSTALLED_APPS = [
# other apps...
'admin_honeypot',
]
Add to your urls.py
:
urlpatterns = [
path('admin/', include('admin_honeypot.urls', namespace='admin_honeypot')),
path('secret-admin/', admin.site.urls), # Your actual admin URL
]
django-admin-sortable2
This package allows you to add drag-and-drop sorting to your models in the admin interface.
Installation
pip install django-admin-sortable2
Add it to your INSTALLED_APPS
:
INSTALLED_APPS = [
# other apps...
'adminsortable2',
]
Usage example:
from django.contrib import admin
from adminsortable2.admin import SortableAdminMixin
from .models import Category
@admin.register(Category)
class CategoryAdmin(SortableAdminMixin, admin.ModelAdmin):
list_display = ('name', 'order')
Your model should include an ordering field:
class Category(models.Model):
name = models.CharField(max_length=200)
order = models.PositiveIntegerField(default=0)
class Meta:
ordering = ['order']
Real-World Example: Building an Enhanced Blog Admin
Let's put several of these packages together to create an enhanced admin interface for a blog application:
First, let's define our models:
# blog/models.py
from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=100)
order = models.PositiveIntegerField(default=0)
class Meta:
ordering = ['order']
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.CASCADE)
published_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
is_published = models.BooleanField(default=False)
def __str__(self):
return self.title
Now, let's create an enhanced admin interface:
# blog/admin.py
from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export import resources
from rangefilter.filters import DateRangeFilter
from adminsortable2.admin import SortableAdminMixin
from admin_auto_filters.filters import AutocompleteFilter
from .models import Category, Post
# Custom filter
class AuthorFilter(AutocompleteFilter):
title = 'Author'
field_name = 'author'
# Resource for import/export
class PostResource(resources.ModelResource):
class Meta:
model = Post
fields = ('id', 'title', 'content', 'category', 'author', 'published_date', 'is_published')
# Admin classes
@admin.register(Category)
class CategoryAdmin(SortableAdminMixin, admin.ModelAdmin):
list_display = ('name', 'order')
@admin.register(Post)
class PostAdmin(ImportExportModelAdmin):
resource_class = PostResource
list_display = ('title', 'category', 'author', 'published_date', 'is_published')
list_filter = (
('published_date', DateRangeFilter),
'category',
AuthorFilter,
'is_published',
)
search_fields = ('title', 'content')
autocomplete_fields = ['author', 'category']
date_hierarchy = 'published_date'
list_editable = ['is_published']
fieldsets = (
('Content', {
'fields': ('title', 'content')
}),
('Categorization', {
'fields': ('category', 'author')
}),
('Publishing', {
'fields': ('is_published',)
}),
)
# Register search fields for the autocomplete functionality
def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super().get_search_results(request, queryset, search_term)
return queryset, use_distinct
With this setup, our blog admin interface now has:
- Drag-and-drop ordering for categories
- Date range filtering for post publication dates
- Autocomplete filtering for authors
- Import/export functionality for posts
- Well-organized fieldsets for post editing
Summary
Django's admin interface is already powerful, but third-party packages can extend it significantly to meet more complex requirements. In this guide, we've covered packages for:
- Enhancing the visual appearance with themes
- Improving filtering and searching capabilities
- Adding import/export functionality
- Implementing security features
- Adding drag-and-drop sorting
These packages can save development time and enhance the user experience for content managers and administrators.
Additional Resources
- Django Packages - Admin Interface - A comprehensive list of admin-related packages
- Django Admin Cookbook - Advanced Django admin techniques
- Awesome Django - A curated list of Django packages and resources
Exercises
- Install django-jazzmin and customize the admin interface for your project.
- Implement django-import-export for a model in your application and try importing data from a CSV file.
- Use django-admin-rangefilter to add date range filtering to a model with timestamp fields.
- Create a custom admin action that performs a bulk operation on selected objects.
- Implement django-admin-honeypot in your project and monitor any attempted logins.
By incorporating these third-party packages into your Django projects, you can create a much more powerful and user-friendly admin interface tailored to your specific requirements.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)