0
0
DjangoHow-ToBeginner · 4 min read

How to Use Inline in Django Admin: Simple Guide

In Django admin, use InlineModelAdmin classes like TabularInline or StackedInline to show related models inline on the parent model's edit page. Register these inline classes inside the parent model's ModelAdmin using the inlines attribute.
📐

Syntax

To use inline in Django admin, create an inline class by subclassing TabularInline or StackedInline. Then add this inline class to the inlines list of the parent model's ModelAdmin.

  • model: The related model to show inline.
  • extra: Number of extra empty forms to display.
  • inlines: List of inline classes in the parent admin.
python
from django.contrib import admin
from .models import ParentModel, ChildModel

class ChildModelInline(admin.TabularInline):
    model = ChildModel
    extra = 1

@admin.register(ParentModel)
class ParentModelAdmin(admin.ModelAdmin):
    inlines = [ChildModelInline]
💻

Example

This example shows how to edit ChildModel objects inline on the ParentModel admin page using TabularInline. You can add, edit, or delete child objects directly while editing the parent.

python
from django.contrib import admin
from .models import Author, Book

class BookInline(admin.TabularInline):
    model = Book
    extra = 2

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline]
Output
In the Django admin, when editing an Author, you will see a table listing related Book entries below the main form. You can add up to 2 new books directly here.
⚠️

Common Pitfalls

  • Forgetting to set the model attribute in the inline class causes errors.
  • Not registering the parent model's admin with the inline results in no inline display.
  • Using TabularInline for complex forms may be less readable; use StackedInline instead.
  • Setting extra too high can clutter the page with empty forms.
python
from django.contrib import admin
from .models import Parent, Child

# Wrong: missing model attribute
class ChildInline(admin.TabularInline):
    extra = 1

@admin.register(Parent)
class ParentAdmin(admin.ModelAdmin):
    inlines = [ChildInline]

# Right:
class ChildInlineCorrect(admin.TabularInline):
    model = Child
    extra = 1

@admin.register(Parent)
class ParentAdminCorrect(admin.ModelAdmin):
    inlines = [ChildInlineCorrect]
📊

Quick Reference

Use this quick guide to remember inline usage:

FeatureDescription
TabularInlineDisplays related objects in a compact table format.
StackedInlineDisplays related objects in stacked blocks, better for complex forms.
modelSpecifies the related model to edit inline.
extraNumber of empty forms shown for adding new related objects.
inlinesList of inline classes added to the parent ModelAdmin.

Key Takeaways

Use TabularInline or StackedInline to show related models inline in Django admin.
Always set the model attribute in your inline class to the related model.
Add your inline class to the parent model's inlines list in its ModelAdmin.
Use extra to control how many empty forms appear for new related objects.
Choose StackedInline for complex forms and TabularInline for simple tabular layouts.