python - Django's ManyToManyField automatically adding a symmetric relationship between objects -


i trying code model holds list of references other objects of same class. using manytomanyfield store references. reason when add element many-to-many field of second element reciprocal relation added first one.

this test model declared

class testmodel(models.model):     foo = models.manytomanyfield('self', null=true) 

then populated database using interactive shell

in [1]: m2m.models import testmodel  in [2]: m1 = testmodel() in [3]: m1.save()  in [4]: m2 = testmodel() in [5]: m2.save() 

i checked m1's , m2's foo field empty.

in [6]: m1.foo.all() out[6]: [] in [7]: m2.foo.all() out[7]: [] 

then added m2 m1.foo

in [8]: m1.foo.add(m2) in [9]: m1.foo.all() out[9]: [<testmodel: testmodel object>] 

and here lost, reason don't understand when appended m2 m1.foo m1 got appended m2.foo.

in [10]: m2.foo.all() out[10]: [<testmodel: testmodel object>]  in [11]: m1.id out[11]: 1  in [12]: m1.foo.get().id out[12]: 2  in [13]: m2.foo.get().id out[13]: 1 

i not expert on django's orm not understanding why relation being automatically established.

is there way avoid behaivour?

use symmetrical argument of manytomanyfield.

https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.manytomanyfield.symmetrical


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -