views:

63

answers:

1

I'm still stuck with the inline Tree-like-eiditing of related models on same page. I've got three models, A, B and C.

Class A

Class B
    fb = foreignkey(A)

Class C
    fc = foreignkey(B)

In admin.py I'm doing something like

AdminA
    inlines = [inlineB]

AdminB
    inlines = [inlineC]

I want that when I edit/add model A, I should be able to add ModelB inline, and add Model B's related Model C entries. I was trying out inlineformsets, but can't find out how to use them for my purpose. Moreover, I found this old discussion on same problem. But again, since I'm new to Django, I don't know how to make it work.

A: 

Its a bit odd answering your own question, but hey nobody else stepped up. And thanks to Bernd for pointing me in right direction. The solution required making an intermediary model. Class BC in my case.

class A(models.Model):                                        
a = models.IntegerField()                                 


class B(models.Model):                                        
    fb = models.ForeignKey(A)                                 
    b = models.IntegerField()                                 

class C(models.Model):                                        
    fc = models.ForeignKey(B)                                 
    c = models.IntegerField()                                 

class BC(models.Model):                                       
    fc = models.ForeignKey(A)                                 
    fb = models.ForeignKey(B)                                 

And instead of having InlineB in Admin of model A, use inline of BC. So full admin.py looks like.

class InlineC(admin.TabularInline):
    model = C
    extra = 1

class BCInline(admin.TabularInline):
    model = BC
    extra = 1

class AdminA(admin.ModelAdmin):
    fieldsets = [
        (None, {
            'fields': ('a',)
            }),
        ]
    inlines = [BCInline]

class AdminB(admin.ModelAdmin):
    fieldsets = [
        (None, {
            'fields': ('b',)
            }),
        ]
    inlines = [InlineC]

And voila, I get button for popus to create full object of B, in the add page of Model A.

Neo