django-nested-inline

raw JSON →
0.4.6 verified Fri May 01 auth: no python

Provides recursive nesting of inline forms in Django Admin. Version 0.4.6 is the latest. Release cadence is low, with occasional updates.

pip install django-nested-inline
error ImportError: cannot import name 'NestedModelAdmin' from 'nested_inline'
cause Using old import path from before v0.4.0.
fix
Use 'from nested_inline.admin import NestedModelAdmin'
error django.core.exceptions.ImproperlyConfigured: 'NestedTabularInline' does not define a 'model' attribute.
cause Missing 'model' attribute on the inline class.
fix
Add 'model = YourModel' to the inline class definition.
breaking In v0.4.x, the import path changed from nested_inline to nested_inline.admin. Old imports will break.
fix Change 'from nested_inline import ...' to 'from nested_inline.admin import ...'
gotcha If you don't use NestedModelAdmin but just ModelAdmin, nested inlines will not render correctly.
fix Ensure your admin class inherits from NestedModelAdmin, not just admin.ModelAdmin.
deprecated The 'inlines' attribute on inline classes (nested inlines) is experimental and may change in future versions.
fix Keep an eye on releases for updates to nested inline declaration syntax.

Define nested models with foreign keys and register admin with NestedModelAdmin to enable recursive inline editing.

# models.py
from django.db import models

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

class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name='children')
    name = models.CharField(max_length=100)

class Grandchild(models.Model):
    child = models.ForeignKey(Child, on_delete=models.CASCADE, related_name='grandchildren')
    name = models.CharField(max_length=100)

# admin.py
from django.contrib import admin
from nested_inline.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline
from .models import Parent, Child, Grandchild

class GrandchildInline(NestedTabularInline):
    model = Grandchild
    extra = 1

class ChildInline(NestedTabularInline):
    model = Child
    extra = 1
    inlines = [GrandchildInline]

class ParentAdmin(NestedModelAdmin):
    inlines = [ChildInline]

admin.site.register(Parent, ParentAdmin)