我从 Django 1.10 升级到 1.11,现在我以前工作的两个模型导致了错误。它们是仅有的两个具有 ManyToManyField
的型号。其中包括related_name
属性。我还有一个ManyToManyField
没有related_name
而且效果很好。
抛出的错误具有误导性:
<class 'hadotcom.admin.CaseStudyAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[0][1]'
我发现其他 SO 帖子引用了该错误,并确认它们都不符合我的问题。
如果我注释掉整行,它就会通过检查。我尝试添加 through
属性,但没有帮助。
示例代码(使用夹层):
class CaseStudyPage(Page):
industries = models.ManyToManyField("IndustryPage", blank=True, related_name="industry_set", through="CaseStudyIndustries")
class CaseStudyAdmin(HaPageAdmin):
inlines = (Foo, Bar,)
很高兴填补任何空白,并提前致谢。
最佳答案
Mezzanine
中的 ContentTypedAdmin
似乎在子类中添加了两次 ManyToMany
字段。我还没有调查过为什么会发生这种情况。一个可能的解决方案是将 ContentTypedAdmin.__init__()
的最后两行更改为:
if not hasattr(field, "translated_field") and field.name not in self.fieldsets[0][1]["fields"]:
self.fieldsets[0][1]["fields"].insert(3, field.name)
关于python - 使用 ManyToManyField 时出现重复字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54336787/