Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin inline for deep relation

It's quite easy to have inline for direct relation, but how can I create inline for deeper relations?

Let's say situation is like

class A(models.Model):
     ...

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

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

Now what I'd like to have is inline in admin for A which will show all of it's Cs with corresponding B name. How do I do that?

like image 578
vartec Avatar asked May 13 '13 12:05

vartec


1 Answers

Take a look at django-nested-inlines. From example:

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)
like image 91
stalk Avatar answered Oct 15 '22 02:10

stalk