Related name for recursive many to many relationship not working

kerryz

Many to many (non-recursive)

class A(models.Model):
    pass

class B(models.Model):
   parents = models.ManyToManyField(A, related_name='children')


>>> A._meta.get_all_field_names()
['children', u'id']

>>> B._meta.get_all_field_names()
[u'id', 'parents']

I can get the sets of children and parents of model instances with a.children.all() and b.parents.all()

Foreign key (recursive)

class FK(models.Model):
    parent = models.ForeignKey('self', related_name='child')


>>> FK._meta.get_all_field_names()
['child', u'id', 'parent']

Any instance of FK will now be able to get both its parent and its child with fk.parent and fk.child

Many to many (recursive)

class M2M(models.Model):
    parents = models.ManyToManyField('self', related_name='children')

>>> M2M._meta.get_all_field_names()
[u'id', 'parents']

One would expect that, like I could access a.children and fk.child, I would also be able to access m2m.children. This seems to not be the case.

How do I access m2m.children?

I'm using Django 1.6.5.


For future reference

As Daniel Roseman's answer said, setting symmetrical=False solves the problem. In a Django ticket it is explained as:

In the case of parent/child, the relationship isn't symmetrical - if A is a child of B, it doesn't follow that A is a parent of B.

With symmetrical=False, the reverse relation specified in the related_name is created just like in the foreign key case:

class M2M(models.Model):
    parents = models.ManyToManyField('self', related_name='children', symmetrical=False)

>>> M2M._meta.get_all_field_names()
[u'id', 'parents', children]


>>> parent.children.add(child)
>>> parent.children.all()  # returns QuerySet containing the child
>>> child.parents.all()    # returns QuerySet containing the parent
Daniel Roseman

You need to set symmetrical=False. As the documentation for ManyToManyField says:

When Django processes this model, it identifies that it has a ManyToManyField on itself, and as a result, it doesn’t add a person_set attribute to the Person class. Instead, the ManyToManyField is assumed to be symmetrical – that is, if I am your friend, then you are my friend.

If you do not want symmetry in many-to-many relationships with self, set symmetrical to False. This will force Django to add the descriptor for the reverse relationship, allowing ManyToManyField relationships to be non-symmetrical.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive relationship on a many to many table

From Dev

Recursive Many to many relationship with Yii

From Dev

Oracle Recursive Join - Many to Many Relationship

From Dev

How many to many relationship in Entity Framework is working

From Dev

Doctrine 2 Many To Many follower relationship not working

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Laravel 5.2.29 Many to Many relationship not working

From Dev

Rails 5: Many-to-Many Relationship Not Working

From Dev

Many-to-Many-to-Many Relationship in SQL Server Analysis Services; Second Many-to-Many Relationship Not Working

From Dev

One to many recursive relationship with Code First

From Dev

GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

From Dev

GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

From Dev

CTE recursive query for two tables that has a many to many relationship

From Dev

Django getting related objects in a many-to-many relationship

From Dev

Obtain list of items related from many to many relationship entity vb

From Dev

Limit related records in polymorphic many-to-many relationship with Laravel

From Dev

How to check if item is related to certain other item in many to many relationship?

From Dev

Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

From Dev

Core Data: Fetching related objects in many-to-many relationship

From Dev

Is this a many-to-many relationship?

From Dev

Many to Many relationship in Ecto

From Dev

Many to many relationship and MySQL

From Dev

implementing a many to many relationship

From Dev

Many to many relationship optimization

From Dev

many to many powerpivot relationship

From Dev

Sqlalchemy many to many relationship

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship with itself

From Dev

Many to Many Relationship in Cakephp

Related Related

  1. 1

    Recursive relationship on a many to many table

  2. 2

    Recursive Many to many relationship with Yii

  3. 3

    Oracle Recursive Join - Many to Many Relationship

  4. 4

    How many to many relationship in Entity Framework is working

  5. 5

    Doctrine 2 Many To Many follower relationship not working

  6. 6

    Laravel 5 | Many to Many Relationship not working

  7. 7

    Laravel 5.2.29 Many to Many relationship not working

  8. 8

    Rails 5: Many-to-Many Relationship Not Working

  9. 9

    Many-to-Many-to-Many Relationship in SQL Server Analysis Services; Second Many-to-Many Relationship Not Working

  10. 10

    One to many recursive relationship with Code First

  11. 11

    GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

  12. 12

    GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

  13. 13

    CTE recursive query for two tables that has a many to many relationship

  14. 14

    Django getting related objects in a many-to-many relationship

  15. 15

    Obtain list of items related from many to many relationship entity vb

  16. 16

    Limit related records in polymorphic many-to-many relationship with Laravel

  17. 17

    How to check if item is related to certain other item in many to many relationship?

  18. 18

    Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

  19. 19

    Core Data: Fetching related objects in many-to-many relationship

  20. 20

    Is this a many-to-many relationship?

  21. 21

    Many to Many relationship in Ecto

  22. 22

    Many to many relationship and MySQL

  23. 23

    implementing a many to many relationship

  24. 24

    Many to many relationship optimization

  25. 25

    many to many powerpivot relationship

  26. 26

    Sqlalchemy many to many relationship

  27. 27

    Laravel - Many to Many relationship

  28. 28

    Many to many relationship with itself

  29. 29

    Many to Many Relationship in Cakephp

HotTag

Archive