Django : Inline editing of related model using inlineformset

坚强是说给别人听的谎言 提交于 2019-12-06 16:49:00

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!