Django中同一个模型有多个多对多关系 [英] Multiple many-to-many relations to the same model in Django

查看:51
本文介绍了Django中同一个模型有多个多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出具有两个多对多关系的以下模型:

Given the following model with two many-to-many relations:

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'.

好;我不需要向后的关系.根据Django文档中的related_name (据我所知,它仅在ForeignKey下),可以设置 related_name ="+" ,并且不会创建向后关系:

Fine; I don't need the backwards relation. According to the Django docs for related_name (which is only under ForeignKey as far as I can see), I can set related_name="+" and backward relations won't be created:

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'.

我需要怎么做才能避免建立反向关系?

What do I need to do to avoid creating reverse relations?

推荐答案

我认为您只需要给两个字段提供不同的related_name:

I think you need to just give the two fields different 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")

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

If you don't give a related name, then it's trying to create the same accessor name (foo_set) twice on the Child model. If you give the same related name, it's again going to try to create the same accessor twice, so you need to give unique related names. With the above code to define your models, then given a Child instance c, you can access related Foo objects with c.bar.all() and c.baz.all().

如果您不想要向后关系,则在每个(唯一的)相关名称后附加一个 + :

If you don't want the backwards relations, then append a + to each of the (unique) related names:

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

这篇关于Django中同一个模型有多个多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆