Django中同一模型的多个多对多关系

时间:2012-12-17 17:26:15

标签: python django django-models

鉴于以下模型具有两个多对多关系:

class Child(models.Model):
    name = models.CharField(max_length=80)

class Foo(models.Model):
    bar = models.ManyToManyField(Child)
    baz = models.ManyToManyField(Child)

这给出了错误:

accounts.foo: Accessor for m2m field 'bar' clashes with related m2m field 'Child.foo_set'. Add a related_name argument to the definition for 'bar'.
accounts.foo: Accessor for m2m field 'baz' clashes with related m2m field 'Child.foo_set'. Add a related_name argument to the definition for 'baz'.

精细;我不需要向后的关系。 According to the Django docs for related_name(据我所见,它只在ForeignKey下),我可以设置related_name="+"并且不会创建后向关系:

class Child(models.Model):
    name = models.CharField(max_length=80)

class Foo(models.Model):
    bar = models.ManyToManyField(Child, related_name="+")
    baz = models.ManyToManyField(Child, related_name="+")

但这不起作用:

accounts.foo: Accessor for m2m field 'bar' clashes with related m2m field 'Child.+'. Add a related_name argument to the definition for 'bar'.
accounts.foo: Reverse query name for m2m field 'bar' clashes with related m2m field 'Child.+'. Add a related_name argument to the definition for 'bar'.
accounts.foo: Accessor for m2m field 'baz' clashes with related m2m field 'Child.+'. Add a related_name argument to the definition for 'baz'.
accounts.foo: Reverse query name for m2m field 'baz' clashes with related m2m field 'Child.+'. Add a related_name argument to the definition for 'baz'.

我需要做些什么来避免产生反向关系?

2 个答案:

答案 0 :(得分:33)

我认为你需要给两个字段赋予不同的related_names:

class Child(models.Model):
  name = models.CharField(max_length=80)

class Foo(models.Model):
  bar = models.ManyToManyField(Child, related_name="bar")
  baz = models.ManyToManyField(Child, related_name="baz")

如果您没有提供相关名称,那么它会尝试在foo_set模型上创建两次相同的访问者名称(Child)。如果您提供相同的相关名称,它将再次尝试创建两次相同的访问者,因此您需要提供唯一的相关名称。使用上面的代码来定义模型,然后给出Child实例c,您可以使用Fooc.bar.all()访问相关的c.baz.all()个对象。

如果您不想要向后关系,请为每个(唯一)相关名称附加+

class Foo(models.Model):
  bar = models.ManyToManyField(Child, related_name="bar+")
  baz = models.ManyToManyField(Child, related_name="baz+")

答案 1 :(得分:14)

你还没有仔细阅读过Django的文档。 Here它说:

  

如果您有多个指向同一模型的ManyToManyField并希望抑制向后关系,请将每个related_name设置为以<+'结尾的唯一值

related_name属性必须是唯一的,不一样。