Inline models let you edit related data together in one place. This makes managing connected information easier and faster.
0
0
Inline models for related data in Django
Introduction
When you want to edit a main item and its related items on the same page in the admin panel.
When you have a parent-child relationship between models and want to add or change children while editing the parent.
When you want to keep related data organized and avoid switching between different pages.
When you want to simplify data entry for related records in the Django admin.
When you want to show related objects directly inside the main object's admin form.
Syntax
Django
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]
TabularInline shows related items in a table format.
You can also use StackedInline for a stacked layout.
Examples
This shows two extra blank forms for adding new related items.
Django
class ChildModelInline(admin.TabularInline): model = ChildModel extra = 2
This uses a stacked layout and shows no extra blank forms.
Django
class ChildModelInline(admin.StackedInline): model = ChildModel extra = 0
This registers the parent model with the inline child model in the admin.
Django
@admin.register(ParentModel)
class ParentModelAdmin(admin.ModelAdmin):
inlines = [ChildModelInline]Sample Program
This example lets you add and edit books directly inside the author admin page. You see a table of books under each author and can add new books easily.
Django
from django.db import models from django.contrib import admin class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) title = models.CharField(max_length=200) def __str__(self): return self.title class BookInline(admin.TabularInline): model = Book extra = 1 @admin.register(Author) class AuthorAdmin(admin.ModelAdmin): inlines = [BookInline]
OutputSuccess
Important Notes
Remember to register your models in admin to see the inline effect.
Use extra to control how many blank forms appear for new related items.
Inline models only work in Django admin, not in regular views.
Summary
Inline models let you edit related data together in Django admin.
Use TabularInline or StackedInline to show related items.
They make managing connected records faster and simpler.