← Back to Django Course | Chapter 8: Django Admin | Lesson 7 of 8

Admin Inlines

Inlines let you edit related records (like a book's chapters) right inside their parent's admin page instead of jumping to a separate screen.

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.

markup
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.

markup
@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.

markup
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. #}
Common Mistakes
  1. Forgetting to set the inline's model FK correctly, causing Django to raise a configuration error at startup.
  2. Using StackedInline for a model with many rows, making the parent's edit page extremely long.
  3. Not setting extra = 0, leaving several confusing blank rows on every edit page by default.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.