使用 ManyToManyField 时重复字段

Duplicate fields when using ManyToManyField

我从 Django 1.10 升级到 1.11,现在我以前工作的两个模型导致了错误。它们是仅有的两个具有包含 related_name 属性的 ManyToManyField 的模型。我有另一个没有 related_nameManyToManyField,它工作正常。

抛出的错误具有误导性:

<class 'hadotcom.admin.CaseStudyAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[0][1]'

我找到了其他引用该错误的 SO 帖子,并确认其中 none 符合我的问题。

如果我注释掉整行,它就通过了检查。我尝试添加 through 属性但没有帮助。

示例代码(使用夹层):

class CaseStudyPage(Page):
  industries = models.ManyToManyField("IndustryPage", blank=True, related_name="industry_set", through="CaseStudyIndustries")

class CaseStudyAdmin(HaPageAdmin):
  inlines = (Foo, Bar,)

很高兴填补任何空白,并提前致谢。

ContentTypedAdmin in Mezzanine 似乎在子类中添加了两次 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)