在InlineAdmin中显示M2M链接模型的选定字段

时间:2022-10-04 14:15:40

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 :

在django应用程序中,我有一个HouseHold模型,有一个孩子ManytoMany外键给孩子模型。在管理员中,我在内联中显示链接的子实例,紧跟着关于该主题的django文档:

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)

我工作得很好但是显示了一个HTML选择小部件,其中包含来自每个家庭的所有孩子(非常长:8000多个项目)

The I tried adding this form to FamilyInline :

我尝试将此表单添加到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)

更新:这里一个非常相似的问题的解决方案不适用,一个错误说Child与HouseHold没有关系(这有些正确,但它有反向的)

1 个解决方案

#1


1  

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 just Child. There actually is a relationship between Household.children.through and Household, but not between Child and Household.

这是基本的M2M功能。内联工作取决于内联对象与正在编辑的父对象直接相关的原理。多对多关系不符合条件,因为子级与父级没有直接关系,而是与本身与父级相关的中间表相关。这就是为什么你可以使用Household.children.through而不仅仅是Child。在Household.children.through和Household之间实际上存在关系,但在Child和Household之间没有关系。

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.

但是,就您而言,您可以通过简单地设计模型来实现您想要的目标。家庭和儿童不是M2M关系。家庭有很多孩子,但孩子只有一个家庭。这意味着家庭应该是孩子的外国关键,然后你就可以轻松地内联孩子。

#1


1  

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 just Child. There actually is a relationship between Household.children.through and Household, but not between Child and Household.

这是基本的M2M功能。内联工作取决于内联对象与正在编辑的父对象直接相关的原理。多对多关系不符合条件,因为子级与父级没有直接关系,而是与本身与父级相关的中间表相关。这就是为什么你可以使用Household.children.through而不仅仅是Child。在Household.children.through和Household之间实际上存在关系,但在Child和Household之间没有关系。

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.

但是,就您而言,您可以通过简单地设计模型来实现您想要的目标。家庭和儿童不是M2M关系。家庭有很多孩子,但孩子只有一个家庭。这意味着家庭应该是孩子的外国关键,然后你就可以轻松地内联孩子。