How deep can "django-nested-admin" have nested inlines?

553 views Asked by At

django-nested-admin shows the example code which has 3 levels "TableOfContentsAdmin", "TocSectionInline" and "TocArticleInline" as shown below:

# An example admin.py for a Table of Contents app

from django.contrib import admin
import nested_admin

from .models import TableOfContents, TocArticle, TocSection

class TocArticleInline(nested_admin.NestedStackedInline): # 3rd Level
    model = TocArticle
    sortable_field_name = "position"

class TocSectionInline(nested_admin.NestedStackedInline): # 2nd Level
    model = TocSection
    sortable_field_name = "position"
    inlines = [TocArticleInline]

class TableOfContentsAdmin(nested_admin.NestedModelAdmin): # 1st Level
    inlines = [TocSectionInline]

admin.site.register(TableOfContents, TableOfContentsAdmin)

Now, how deep can django-nested-admin have nested inlines? Only 3 levels?

1

There are 1 answers

0
Super Kai - Kazuya Ito On BEST ANSWER

6 levels for me. But, the deeper levels are created, the slower the page is displayed.

First, I created 10 models as shown below:

# "models.py"

from django.db import models

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

class Second(models.Model):
    name = models.CharField(max_length=100)
    first = models.ForeignKey(First, on_delete=models.PROTECT)

class Third(models.Model):
    name = models.CharField(max_length=100)
    second = models.ForeignKey(Second, on_delete=models.PROTECT)

class Fourth(models.Model):
    name = models.CharField(max_length=100)
    third = models.ForeignKey(Third, on_delete=models.PROTECT)

class Fifth(models.Model):
    name = models.CharField(max_length=100)
    fourth = models.ForeignKey(Fourth, on_delete=models.PROTECT)

class Sixth(models.Model):
    name = models.CharField(max_length=100)
    fifth = models.ForeignKey(Fifth, on_delete=models.PROTECT)

class Seventh(models.Model):
    name = models.CharField(max_length=100)
    sixth = models.ForeignKey(Sixth, on_delete=models.PROTECT)

class Eighth(models.Model):
    name = models.CharField(max_length=100)
    seventh = models.ForeignKey(Seventh, on_delete=models.PROTECT)

class Ninth(models.Model):
    name = models.CharField(max_length=100)
    eighth = models.ForeignKey(Eighth, on_delete=models.PROTECT)

class Tenth(models.Model):
    name = models.CharField(max_length=100)
    ninth = models.ForeignKey(Ninth, on_delete=models.PROTECT)

Then, created 10 levels as shown below:

# "admin.py"

from nested_admin import NestedTabularInline, NestedModelAdmin
from .models import (
    First, 
    Second, 
    Third, 
    Fourth, 
    Fifth,
    Sixth,
    Seventh,
    Eighth,
    Ninth,
    Tenth
)

class TenthInline(NestedTabularInline): # 10th level
    model = Tenth

class NinthInline(NestedTabularInline): # 9th level
    model = Ninth
    inlines = (TenthInline,)

class EighthInline(NestedTabularInline): # 8th level
    model = Eighth
    inlines = (NinthInline,)

class SeventhInline(NestedTabularInline): # 7th level
    model = Seventh
    inlines = (EighthInline,)

class SixthInline(NestedTabularInline): # 6th level
    model = Sixth
    inlines = (SeventhInline,)

class FifthInline(NestedTabularInline): # 5th level
    model = Fifth
    inlines = (SixthInline,)

class FourthInline(NestedTabularInline): # 4th level
    model = Fourth
    inlines = (FifthInline,)

class ThirdInline(NestedTabularInline): # 3rd level
    model = Third
    inlines = (FourthInline,)

class SecondInline(NestedTabularInline): # 2nd level
    model = Second
    inlines = (ThirdInline,)

@admin.register(First)             
class FirstInline(NestedModelAdmin): # 1st level
    inlines = (SecondInline,)

But, when clicking on "ADD FIRST" button, I got errors. In addition, I tried 9, 8 and 7 levels but, I got errors as well:

enter image description here

Finally, I tried 6 levels as shown below:

# "admin.py"

from nested_admin import NestedTabularInline, NestedModelAdmin
from .models import (
    First, 
    Second, 
    Third, 
    Fourth, 
    Fifth,
    Sixth,
)

class SixthInline(NestedTabularInline):
    model = Sixth

class FifthInline(NestedTabularInline):
    model = Fifth
    inlines = (SixthInline,)

class FourthInline(NestedTabularInline):
    model = Fourth
    inlines = (FifthInline,)

class ThirdInline(NestedTabularInline):
    model = Third
    inlines = (FourthInline,)

class SecondInline(NestedTabularInline):
    model = Second
    inlines = (ThirdInline,)

@admin.register(First)
class FirstInline(NestedModelAdmin):
    inlines = (SecondInline,)

Then, I could display 6 levels as shown below but it's very slow to be displayed:

enter image description here