【问题标题】:Duplicate fields when using ManyToManyField使用 ManyToManyField 时字段重复
【发布时间】:2019-01-23 22:39:31
【问题描述】:

我从 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,)

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

【问题讨论】:

  • 如果"IndustryPage" 是模型,则删除引号。
  • 感谢@Ahtisham,但我将它放在引号中,以便我可以延迟加载它,因为它稍后在文件中定义。
  • 向我们展示CaseStudyAdmin 代码。
  • 所见即所得。这两个内联是基本模型,即使我删除它们也会引发相同的错误。

标签: python django mezzanine


【解决方案1】:

似乎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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 2013-08-03
    • 1970-01-01
    • 2019-02-06
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多