Django:使用inlineformset进行相关模型的内联编辑 [英] Django : Inline editing of related model using inlineformset
问题描述
我仍然停留在相同页面上的相关模型的inline Tree-like-eiditing。
我有三个模型A,B和C.
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)
在admin.py我正在做一些类似
In admin.py I'm doing something like
AdminA
inlines = [inlineB]
AdminB
inlines = [inlineC]
我想要当我编辑/添加模型A时,我应该可以添加ModelB内联,并添加模型B相关的模型C条目。我正在尝试内联格式,但是找不到如何将它们用于我的目的。此外,我发现这个关于同样问题的旧讨论一>。但是,再次,由于我是Django的新手,我不知道如何使其工作。
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.
推荐答案
它有点奇怪的回答自己的问题,但没有人加紧。感谢Bernd指出我正确的方向。该解决方案需要制定中介模式。在我的情况下BC类。
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)
而不是在模型管理中使用InlineB A,使用BC内联。所以完整的admin.py看起来像。
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]
瞧,我得到按钮,用于在A型的添加页面中创建B的完整对象。
And voila, I get button for popus to create full object of B, in the add page of Model A.
这篇关于Django:使用inlineformset进行相关模型的内联编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!