Django Many-to-Many (m2m) Relation to same model

PythonDjangoDjango ModelsMany to-Many

Python Problem Overview


I'd like to create a many-to-many relationship from and to a user class object.

I have something like this:

class MyUser(models.Model):
    ...
    blocked_users = models.ManyToManyField(MyUser, blank=True, null=True)

The question is if I can use the class reference inside itself. Or do I have to use "self" insead of "MyUser" in the ManyToManyField? Or is there another (and better) way to do it?

Python Solutions


Solution 1 - Python

Technically, I'm pretty sure "MyUser" or "self" will work, as long as it's a string in either case. You just can't pass MyUser, the actual class.

However, the docs always use "self". Using "self" is not only more explicit about what's actually happening, but it's impervious to class name changes. For example, if you later changed MyUser to SomethingElse, you would then need to update any reference to "MyUser" as well. The problem is that since it's a string, your IDE will not alert you to the error, so there's a greater chance of your missing it. Using "self" will work no matter what the class' name is now or in the future.

Solution 2 - Python

class MyUser(models.Model):
    ...
    blocked_users = models.ManyToManyField("self", blank=True)

Solution 3 - Python

Don't forget use symmetrical=False, if you use .clear() or .add() method for related objects and don't wanna object on other side of relation update own data in relation field.

some_field = models.ManyToManyField('self', symmetrical=False)

Solution 4 - Python

I think it should be class name instead of self. because with using self like this

parent = models.ManyToManyField('self', null=True, blank=True)

when i add parent:

user1.parent.add(user2)

i have 2 record in database like this: enter image description here

and with using class name liken this:

parent = models.ManyToManyField('User', null=True, blank=True)

i have one record in database like this: enter image description here

note that i use uuid for pk and i use django 3.1

EDIT: as @shinra-tensei explained as comment in this answer we have to set symmetrical to False if we use self. documented in Django Documents: ManyToManyField.symmetrical

Solution 5 - Python

If you use self or MyUser you will get a NameError in both cases. You should write "self" as string. See the example below:

class MyUser(models.Model):
    ...
    blocked_users = models.ManyToManyField("self", blank=True, null=True)

And do not forget to set the symmetrical attribute to False if the relationship is not symmetrical.

For further details check: https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ManyToManyField

Solution 6 - Python

don't use 'self' in ManyToManyField, it will cause you object link each other, when use django form to submit it

class Tag(models.Model):
    ...
    subTag = models.ManyToManyField("self", blank=True)

 ...
 aTagForm.save()

and result:

 a.subTag == b
 b.subTag == a

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRonView Question on Stackoverflow
Solution 1 - PythonChris PrattView Answer on Stackoverflow
Solution 2 - PythonGoinView Answer on Stackoverflow
Solution 3 - PythonDmitryBaraView Answer on Stackoverflow
Solution 4 - PythonsahamaView Answer on Stackoverflow
Solution 5 - PythonFrancisco RevillaView Answer on Stackoverflow
Solution 6 - PythonruandaoView Answer on Stackoverflow