django 中的多态内联模型形式
我有一个人物模型,其中有许多动物模型作为宠物。狗是具有“最喜欢的骨头”字段的动物,而猫是具有“喜欢猫薄荷?”字段的动物。场和“最喜欢的鱼”场。
#models
class Person(db.model):
pass
class Animal(db.model):
models.ForeignKey(Person) #owner
name = CharField()
class Dog(Animal):
favorite_bone = CharField()
class Cat(Animal):
favorite_fish = CharField()
likes_catnip = BooleanField()
我想在 Person 管理表单中内联编辑所有 Persons 宠物,但是,我读到 Django 内联管理表单不支持多态内联表单 [1],因此,您只能获得父类字段(例如,不是 favorite_bone 或 favorite_fish 和 likes_catnip 字段。
这个问题从何而来?
可以对框架进行哪些更改来适应此问题?
如果不应该进行这些更改,为什么不呢?
I have a Person model which has many Animal models as pets. Dog is an Animal with a "favorite bone" field, and Cat is an Animal with a "likes catnip?" field and a "favorite fish" field.
#models
class Person(db.model):
pass
class Animal(db.model):
models.ForeignKey(Person) #owner
name = CharField()
class Dog(Animal):
favorite_bone = CharField()
class Cat(Animal):
favorite_fish = CharField()
likes_catnip = BooleanField()
I would like to inline edit all of a Persons pets, in the Person admin form however, I've read that Django inline admin forms don't support polymorphic inline forms[1], in that, you will only get the parent class fields (e.g. not the favorite_bone or favorite_fish and likes_catnip fields.
Where does this problem come from?
What changes could be made to the framework to accommodate this?
If these changes should not be made, why not?
[1] http://www.mail-archive.com/[email protected]/msg66410.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(这是一个老问题,但我想我应该添加一个答案,以防它仍然有用。我最近一直在研究类似的问题。)
我相信改变 Django 表单生成来执行以下操作将具有挑战性你想要的。原因是内联表单集对内联的所有行使用单个类/表单——没有针对内联表单的每行进行评估的配置选项。我通过阅读代码本身确信了这一点——在 django.contrib.admin.options.py 中查找“inline”和“formset”,特别是第 1039-1047 行(版本 1.5.1)。这也是为什么您不能让某些字段在现有项目中为只读而在新项目中可更改的原因(请参阅这个问题,例如)。
针对只读情况找到的解决方法涉及一个自定义小部件,该小部件可产生所需的行为,例如 这个。然而,这仍然不能直接支持多态性。我认为您最终需要将不同的类型映射回共同的祖先(例如,让所有宠物类都能够返回其独特属性和值的字典),然后创建一个自定义小部件来呈现多态部分你。然后,您必须在保存时将这些值映射回。
这可能比它的价值更具挑战性,并且可能会导致回到另一个答案中的建议,不要为此使用管理员:-)
(This is an old question, but I thought I'd add an answer in case it is still useful. I've been working on a similar question recently.)
I believe it would be challenging to change Django form-generation to do what you want. The reason is that the inline formset uses a single class/form for all rows of the inline -- there are no configuration options that are evaluated per-row of the inline form. I have convinced myself of this by reading the code itself --- look for "inline" and "formset" in django.contrib.admin.options.py, especially lines 1039-1047 (version 1.5.1). This is also the reason why you can't have some fields read-only in existing items and changeable in new items (see this SO question, for example).
The workarounds found for the readonly case have involved a custom widget that produces the desired behavior such as this one. That still won't directly support polymorphism, however. I think you would need to end up mapping your divergent types back to a common ancestor (e.g. have all pet classes able to return a dict of their unique attributes and values), and then create a single custom widget that renders out the polymorphic part for you. You'd then have to map the values back on save.
This might be more challenging than it is worth, and may lead back to the suggestion in the other answer to not use admin for this :-)
可以看看这里。
但我认为模型管理员目前无法做这样的事情。
您可以为您的模型创建自定义编辑视图...
几乎一切皆有可能。
may have a look here.
but i think the modeladmin is currently not able todo such things.
you are able to create a custom edit view for your model...
there is almost everything possible.
可以使用通用关系来做到这一点。
It may be possible to do this with Generic Relations.