Admin Inlines
In this page:
What Inlines Solve
Without inlines, editing a Book's related Chapter rows means navigating to the separate Chapter admin page for each one. Inlines embed that editing directly on the Book's own edit page.
Example: What Inlines Solve
Defines a compact, table-style inline for Chapter rows related to a Book.
from django.contrib import admin
from .models import Book, Chapter
class ChapterInline(admin.TabularInline):
model = Chapter
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Attaching an Inline to a ModelAdmin
The inline class is attached to its parent's ModelAdmin via the inlines attribute, not registered on its own.
Example: Attaching an Inline to a ModelAdmin
Now editing a Book in the admin shows its Chapter rows in an embedded table.
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
inlines = [ChapterInline]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Controlling Blank Rows with extra
By default Django adds a few empty rows for creating new related objects. Setting extra to a small number (or 0) keeps the form from getting cluttered.
Note: Use StackedInline instead of TabularInline when each related row has many fields that don't fit well in a table.
Example: Controlling Blank Rows with extra
Shows existing Chapter rows plus exactly one blank row for adding a new one.
class ChapterInline(admin.TabularInline):
model = Chapter
extra = 1
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting to set the inline's model FK correctly, causing Django to raise a configuration error at startup.
- Using StackedInline for a model with many rows, making the parent's edit page extremely long.
- Not setting extra = 0, leaving several confusing blank rows on every edit page by default.
- TabularInline shows related rows in a compact table; StackedInline shows them as separate stacked forms.
- Inlines are attached to a parent ModelAdmin via the inlines list.
- extra controls how many blank rows for adding new related records are shown.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: