A stacked inline form lets Django Admin edit related model data vertically inside a parent object's form. For a user profile, it keeps account details and profile fields on one Add User screen. section 6.2.2
Configure the inline in three practical steps:
- Create a class inheriting from admin.StackedInline and set model to the related profile model. section 6.2.2
- Add the inline to a custom UserAdmin class through its inlines attribute. section 6.2.2
- Unregister Django's default User admin, register the customized version, then run makemigrations and migrate. section 6.2.2
Listing 6.2 Code for managing your UserProfile class through the Django Admin
# RiffMates/bands/admin.py
...
from django.contrib.auth.admin import UserAdmin as
BaseUserAdmin #1
from django.contrib.auth.models import User #2
...
from bands.models import Musician, Band, Venue, Room,
UserProfile #3
...
class UserProfileInline(admin.StackedInline): #4
model = UserProfile #5
can_delete = False
class UserAdmin(BaseUserAdmin): #6
inlines = [UserProfileInline] #7
admin.site.unregister(User) #8
admin.site.register(User, UserAdmin)
The configuration connects UserProfile to the User administration screen and disables deletion of the profile inline. section 6.2.2
