Inline Models in Admin

A snippet showing how to use inlines in Django admin.

# models.py

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=120)
    email = models.EmailField()

    def __str__(self):
        return self.name

class Book(models.Model):
    title = models.CharField(max_length=200)
    published_date = models.DateField()
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')

    def __str__(self):
        return self.title
  
# admin.py

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

class BookInline(admin.TabularInline):
    model = Book
    extra = 1  # how many empty forms to show

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('name', 'email')
    inlines = [BookInline]

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_date')
  
Explanation:
  • Use TabularInline or StackedInline to embed related models directly inside the parent form.
  • Inlines are useful for one‑to‑many relationships like Author → Books, Category → Products, etc.
  • extra defines how many blank rows to display for new related objects.
  • Keep the number of inlines manageable; too many related rows can slow down form rendering.
  • Category Admin
  • Total Views 577
  • Last Modified 05 October, 2025
  • Tags #admin #inlines #models #customization
Previous snippet
Add Filters in Admin
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.