English 中文(简体)
在InlineAdmin中显示M2M连接模型的某些领域
原标题:Displaying selected fields of a M2M linked model in an InlineAdmin

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)

我试图将这一表格添加到家庭信息:

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

我只想向每个孩子展示一些田地(第一名称、最后名称、学校......)而不是这一选择的植被。 是否可能?

更新: 这里一个非常相似的问题的解决办法不适用。

最佳回答

页: 1 M2M在此发挥功能。 在座标与正在编辑的母物体直接相关。 由于儿童与父母没有直接关系,而是与父母亲有关系的一个中间表格有关,因此,许多与男子的关系并不合适。 因此,你可以使用<代码>Household.children. through,而不仅仅是Child。 实际上,在<代码>Household.children. through 和Household之间有关系,但在ChildHousehold 之间没有关系。

不过,为了你的目的,你可以首先妥善设计这些模式,从而实现你所希望的目标。 家庭和儿童的关系not。 家庭中有许多孩子,但只有一名孩子。 这意味着家庭应当成为关于儿童问题的外在家庭,然后,你可以很容易地在<条码>上填写。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签