Admin Unfold
1. What Is Django Unfold?
Key Advantages
- Modern Look & Feel: Provides an updated design over the default Django Admin.
- Collapsible Sections & Tabs: Makes large or complex forms more manageable.
- Better Layout & Navigation: Simplifies admin pages to reduce clutter.
- Configurable & Extensible: Easily override templates or add custom CSS/JS.
- Integration with Default Admin: Minimal disruption to your existing admin code.
2. Installation & Setup
Install the Package:
pip install django-unfold
Or if you use Poetry:
poetry add django-unfold
Add to
INSTALLED_APPS
:# settings.py = [ INSTALLED_APPS # ... 'django_unfold', ]
Run Migrations (if prompted, though Unfold typically doesn’t add models):
python manage.py migrate
Collect Static:
python manage.py collectstatic
This ensures all static files related to Django Unfold’s styling and scripts are in place.
Test:
python manage.py runserver
Log into your admin. You should notice a more refined UI.
3. Using Unfold in Your Admin
A. Subclass the Unfold Admin
By default, you can get many enhancements just by using UnfoldAdmin
instead of Django’s ModelAdmin
:
# myapp/admin.py
from django_unfold.admin import UnfoldAdmin
from django.contrib import admin
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(UnfoldAdmin):
= ('id', 'name', 'created_at')
list_display = ('name',) search_fields
This typically gives you: - A cleaner form layout - Improved inlines handling - Enhanced styling out of the box
B. Collapsible Fieldsets
Similar to Django’s default fieldsets
, you can make them collapsible for better organization:
class MyModelAdmin(UnfoldAdmin):
= [
fieldsets 'Basic Info', {
('fields': ('name', 'description'),
'classes': ('collapse',),
}),'Additional Info', {
('fields': ('some_other_field',),
}), ]
C. Using Tabs
Unfold provides a tabbed layout option to group fields:
class MyModelAdmin(UnfoldAdmin):
= [
tabs 'General', {
('fields': ('name', 'description'),
}),'Details', {
('fields': ('some_other_field', 'another_field'),
}), ]
When you load the change form page in the admin, the fields appear under tabbed sections instead of a single vertical form.
D. Custom List Displays
Leverage Unfold’s enhanced list views (e.g., improved table responsiveness):
class MyModelAdmin(UnfoldAdmin):
= ('id', 'name', 'status')
list_display = ('status', 'created_at')
list_filter = ('name',) search_fields
E. Admin-Site-Wide Settings
If you want to override the admin site headers or site titles:
# myproject/admin.py
from django.contrib import admin
= "My Unfold Admin"
admin.site.site_header = "Unfold Admin Portal"
admin.site.site_title = "Welcome to My Unfold Admin" admin.site.index_title
4. Advanced Customization
A. Extending UnfoldAdmin
If you want a custom base admin for all your models:
# myapp/admin_bases.py
from django_unfold.admin import UnfoldAdmin
class BaseUnfoldAdmin(UnfoldAdmin):
# e.g. add custom media, override default behaviors
class Media:
= {
css 'all': ('css/custom_admin.css',)
}= ('js/custom_admin.js',)
js
# You can override built-in methods or add custom logic here
def some_custom_method(self, obj):
return "Custom Display"
Then in admin.py
:
from .admin_bases import BaseUnfoldAdmin
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(BaseUnfoldAdmin):
pass
B. Template Overrides
Django Unfold references certain admin templates like admin/base.html
or admin/change_form.html
. You can override these by creating templates in your project:
myproject/
templates/
admin/
base.html
change_form.html
...
If you want to specifically override Unfold’s template logic, check the Unfold documentation or source to see which template names it’s using. Usually, it’s the same structure as Django’s admin templates, so standard overrides typically apply.
C. Enhanced Inlines
Inline forms for related models can also benefit from Unfold’s layout. Simply swap admin.TabularInline
or admin.StackedInline
with the equivalents from Unfold if provided, or keep them as is—Unfold’s CSS typically styles them automatically.
5. Configuration & Settings
Some versions of Unfold might expose settings in settings.py
to enable or disable specific features. For example:
# settings.py
= {
UNFOLD_SETTINGS 'ENABLE_RESPONSIVE_TABLES': True,
'USE_TABS': True,
'THEME': 'default', # or 'dark', etc., if multiple themes are available
}
Check the project’s documentation for the exact keys and possible values. (Not all releases of Unfold have these settings, so it depends on your version.)
6. Combining Unfold with Other Admin Tools
Django Unfold can coexist with other admin enhancements like: - django-extensions: Greatly expands your management commands. - django-import-export: Enables data import/export from the admin. - django-grappelli or django-suit: However, mixing admin UI frameworks often causes conflicts in CSS or JavaScript. Generally, you pick one UI library (Unfold, Grappelli, Suit, etc.) to avoid collisions.
If you do combine them, be sure to test thoroughly and override CSS if needed to prevent style conflicts.
7. Potential Limitations
- Less Common: It’s not as widely used as Grappelli or Django Suit, so there might be fewer community snippets.
- Version Compatibility: Always check if the Unfold version supports your Django version (especially if you’re on newer Django releases).
- Template Collisions: If you have heavy template customizations for the admin, you may need to reconcile them with Unfold’s approach.
- Development Activity: Check the repository’s activity for updates or potential issues with stale code.
8. Quick Reference Table
Feature | Usage |
---|---|
Installation | pip install django-unfold |
Admin Import | from django_unfold.admin import UnfoldAdmin |
Basic Usage | class MyModelAdmin(UnfoldAdmin): ... |
Collapsible Fieldsets | fieldsets = [(... {'classes': ('collapse',)})] |
Tabbed Layout | tabs = [("Tab1", {...}), ("Tab2", {...})] |
Template Overrides | Place overrides in templates/admin/ (same as default Django admin) |
Custom Theme | Adjust UNFOLD_SETTINGS in settings.py or override CSS via Media class |
Registration | @admin.register(MyModel) or admin.site.register(MyModel, MyModelAdmin) |
9. Conclusion & Best Practices
- Start Simple
- Swap in
UnfoldAdmin
for your existingModelAdmin
to see immediate UI improvements.
- Swap in
- Organize Fields
- Use collapsible fieldsets or tabs to manage complex models with many fields.
- Override Responsibly
- Use the standard Django approach to override admin templates only if you need deep layout changes.
- Maintain Code Consistency
- If you adopt Unfold, consider making it the default for all your
ModelAdmin
s to have a consistent admin UI.
- If you adopt Unfold, consider making it the default for all your
- Check Documentation
- If you run into styling issues or need advanced features, consult the official GitHub/Docs for up-to-date instructions.
By following these steps and guidelines, you can transform your Django Admin into a more modern, user-friendly, and efficient environment using Django Unfold.