python - 使用 ManyToManyField 时出现重复字段

标签 python django mezzanine

我从 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/

相关文章:

django测试客户端总是返回301

python - Whitenoise、Mezzanine、Django -ImportError : cannot import name ManifestStaticFilesStorage

python - 与 re.findall 有问题

django - 如何在 Django 1.9 中将更新加入 postgres 查询作为查询集编写?

python - Django 中的链接

django - 使用 Selenium webdriver 测试在 Travis CI 中使用 Sauce Labs 的 SSL 错误(Django 项目)

django - 与 angularjs 一起使用 mezzanine

python - 将 csv 上传到大查询时添加日期加载字段

python - QTextEdit 在调整大小时不显示水平滚动条

python - Django 信号接收器接受 self 参数