Django管理员内联深层关系

时间:2013-05-13 12:54:26

标签: django django-admin

内联直接关系很容易,但是如何为更深层次的关系创建内联?

让我们说情况就像

class A(models.Model):
     ...

class B(models.Model):
     a = models.ForeignKey(A)

class C(models.Model):
     b = models.ForeignKey(B)

现在我想要的是A的内联管理员,它会显示所有CB的{​​{1}}名称。我该怎么做?

1 个答案:

答案 0 :(得分:4)

看看django-nested-inlines。例如:

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C

class MyNestedInline(NestedTabularInline):
    model = C

class MyInline(NestedStackedInline):
    model = B
    inlines = [MyNestedInline,]

class MyAdmin(NestedModelAdmin):
    pass

admin.site.register(A, MyAdmin)