django nested inlines

  1. Define models:
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

class Page(models.Model):
    content = models.TextField()
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
  1. Create inline admin classes:
from django.contrib import admin

class PageInline(admin.StackedInline):
    model = Page

class BookInline(admin.TabularInline):
    model = Book
    inlines = [PageInline]

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline]

class BookAdmin(admin.ModelAdmin):
    inlines = [PageInline], AuthorAdmin), BookAdmin)
  1. Register models with admin:, AuthorAdmin), BookAdmin)

Now, when you access the Author, Book, or Page models in the Django admin, you'll see nested inlines for Books within Authors and Pages within Books.