pythondjangomezzanine

Duplicate fields when using ManyToManyField


I upgraded from Django 1.10 to 1.11 and now two of my models which previously worked are causing errors. They are the only two models that have a ManyToManyField that includes a related_name attribute. I have another ManyToManyField without a related_name and it works fine.

The error that gets thrown is misleading:

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

I've found other SO posts that reference that error and confirmed that none of them match my issue.

If I comment out the entire line it passes the check. I tried adding a through attribute and that didn't help.

Sample code (using Mezzanine):

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

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

Happy to fill in any blanks, and thanks in advance.


Solution

  • It seems like ContentTypedAdmin in Mezzanine adds ManyToMany fields in subclasses twice. I haven't investigated exactly why it happens. A possible solution is to alter the last two lines of ContentTypedAdmin.__init__() to:

    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)