liveBook · Manning

Stacked inline form

As explained in Django in Action

admin.StackedInline is Django's base administrative class for defining vertically stacked inline forms for related model objects.

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

How Django in Action covers it

How deep it goes
Explains inheritance, model assignment, inline registration, admin replacement, and migration steps without descending into implementation internals.
Examples
Demonstrates a UserProfile inline containing musician profiles and controlled venues on the Add User screen.
What it assumes
Assumes a related UserProfile model and teaches the required admin wiring from Django's provided base class.
What you can do after
Operate/configure Django Admin to edit a user's related profile data from the same account form.
How it connects
Connects Django Admin with UserAdmin, UserProfile, one-to-one relationships, and ORM migrations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.