Django Nested Admin

4.1.6 · active · verified Sun Apr 12

django-nested-admin provides Django admin classes that allow for deeply nested inlines within the Django administration interface. It extends Django's native `InlineModelAdmin` functionality to support multiple levels of related objects. The current version is 4.1.6, and it typically releases new versions to maintain compatibility with new Django releases.

Warnings

Install

Imports

Quickstart

To use `django-nested-admin`, define your models, then create `NestedTabularInline` or `NestedStackedInline` classes for your child models, nesting them as needed. Finally, register your top-level model with `NestedModelAdmin` in your `admin.py` file. This example demonstrates a three-level nesting: Grandparent -> Parent -> Child.

from django.contrib import admin
from django.db import models

from nested_admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline

# Example Models
class Grandparent(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Parent(models.Model):
    grandparent = models.ForeignKey(Grandparent, on_delete=models.CASCADE, related_name='parents')
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

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

    def __str__(self):
        return self.name


# Admin Inlines
class ChildInline(NestedTabularInline):
    model = Child
    extra = 1

class ParentInline(NestedStackedInline):
    model = Parent
    inlines = [ChildInline]
    extra = 1

@admin.register(Grandparent)
class GrandparentAdmin(NestedModelAdmin):
    inlines = [ParentInline]
    list_display = ('name',)

# Register Parent and Child models to ensure they appear in admin if not directly inline
# admin.site.register(Parent)
# admin.site.register(Child)

view raw JSON →