Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple many-to-many relations to the same model in 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) 

This gives the error:

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

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="+") 

This doesn't work though:

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?

like image 763
Wilfred Hughes Avatar asked Dec 17 '12 17:12

Wilfred Hughes


People also ask

How does Django handle many-to-many relationship?

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.

What is many-to-many field in Django?

A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.

Does Django have many relationships?

One to many relationships in Django models.A one to many relationship implies that one model record can have many other model records associated with itself. For example, a Menu model record can have many Item model records associated with it and yet an Item belongs to a single Menu record.


Video Answer


1 Answers

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") 

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+") 
like image 186
Emily Avatar answered Oct 09 '22 09:10

Emily