在 InlineAdmin 中显示 M2M 链接模型的选定字段
在 django 应用程序中,我有一个 HouseHold
模型,其中有一个 children
ManytoMany 外键到 Child
模型。
在管理中,我在内联中显示链接的 Child
实例,紧随 关于该主题的 django 文档:
class FamilyInline(admin.TabularInline):
model = HouseHold.children.through
class HouseHoldAdmin(admin.ModelAdmin):
inlines = [ FamilyInline, ]
exclude = ('children',)
我工作得很好,但显示了一个 HTML 选择小部件,其中包含每个家庭的所有孩子(这非常长的 : 8000+ 个项目)
我尝试将此表单添加到 FamilyInline :
class ChildForm(forms.ModelForm):
class Meta:
model = Child
fields = ('name','school')
class FamilyInline(admin.TabularInline):
form = ChildForm
model = HouseHold.children.through
但这会引发错误:
FieldError at /admin/myapp/household/820126/
Unknown field(s) (name, school) specified for HouseHold_children
我只想为每个孩子显示编辑行的一些字段(名字、姓氏、学校...)而不是这个选择小部件。是否可以 ?
更新:解决方案这里非常相似的问题不适用,错误说 Child 与 HouseHold 没有关系(这在某种程度上是正确的,但它有一个相反的关系)
In a django app, I have a HouseHold
model, with a children
ManytoMany Foreign key to a Child
Model.
In the admin, I'm showing the linked Child
instances in an inline, following closely django docs on the subject :
class FamilyInline(admin.TabularInline):
model = HouseHold.children.through
class HouseHoldAdmin(admin.ModelAdmin):
inlines = [ FamilyInline, ]
exclude = ('children',)
I works well but shows a HTML select widget, containing all children from every households (which is very long : 8000+ items)
The I tried adding this form to FamilyInline :
class ChildForm(forms.ModelForm):
class Meta:
model = Child
fields = ('name','school')
class FamilyInline(admin.TabularInline):
form = ChildForm
model = HouseHold.children.through
But this throws an error :
FieldError at /admin/myapp/household/820126/
Unknown field(s) (name, school) specified for HouseHold_children
I just wanted to show for each children edit line some of its fields (first name, last name, school...) instead of this select widget. Is it possible ?
update : A solution for a very similar question here is not applicable, an error saying that Child has no relation with HouseHold (which is somewhat true, but it has a reverse one)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是这里的基本 M2M 功能。内联的原则是内联对象与正在编辑的父对象直接相关。多对多关系不符合条件,因为子项不直接与父项相关,而是与本身与父项相关的中间表相关。这就是为什么您可以使用
Household.children.through
而不仅仅是Child
。实际上,Household.children.through
和Household
之间存在关系,但Child
和Household
之间没有关系。不过,出于您的目的,您可以通过首先正确设计模型来实现您想要的目标。家庭和儿童不是 M2M 关系。一个家庭有很多孩子,但一个孩子却只有一个家庭。这意味着 family 应该是 child 的外键,然后您就可以轻松内联
Child
。This is basic M2M functionality here. Inlines work off the principal that the inlined object is directly related to the parent object being edited. A many-to-many relationship doesn't qualify, as the child is not directly related to the parent, but rather related to an intermediary table that itself is related to the parent. That's why you can use
Household.children.through
and not justChild
. There actually is a relationship betweenHousehold.children.through
andHousehold
, but not betweenChild
andHousehold
.For you purposes, though, you can achieve what you want by simply designing the models properly in the first place. Household and Children is not a M2M relationship. A Household has many children, but a child has only one household. That means that household should be a ForeignKey on child, and then you'll be able to inline
Child
easily.