Django:related_name反向访问器

时间:2014-12-04 09:50:08

标签: python django

我有这个模型,其中InstitutePerson是Person的子类。

    • InstitutePerson
  • 项目

在项目中:

participants_institite = models.ManyToManyField(InstitutePerson, blank = True, null = True)
participants_exterior = models.ManyToManyField(Person, blank = True, null = True)

我收到错误:

Project.participants_institute: (fields.E304) Reverse accessor for 'Project.participants_institute' clashes with reverse accesor for 'Project.participants_exterior'.

我认为related_name可以解决问题,但在看到一些帖子(related_name argument not working as expected in Django model?)之后,由于类之间的继承,我不知道如何继续。

1 个答案:

答案 0 :(得分:0)

使用related_name arg并手动定义它 https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

如果你有两个相同类型的M2M字段,Django无法自动在目标模式上生成属性。

如果你不是相反的方向,你可以简单地说是

models.ManyToManyField(..., related_name='+r1') models.ManyToManyField(..., related_name='+r2')

相关问题