108👍
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.
60👍
class MyUser(models.Model):
...
blocked_users = models.ManyToManyField("self", blank=True)
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Are sessions needed for python-social-auth
- [Django]-Where can I find the error logs of nginx, using FastCGI and Django?
24👍
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)
- [Django]-How to validate an Email address in Django?
- [Django]-Django Generic Views using decorator login_required
- [Django]-Django auto_now and auto_now_add
6👍
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:
and with using class name liken this:
parent = models.ManyToManyField('User', null=True, blank=True)
i have one record in database like this:
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
- [Django]-Does django with mongodb make migrations a thing of the past?
- [Django]-Django Rest Framework partial update
- [Django]-Django – Website Home Page
3👍
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
- [Django]-Set Django's FileField to an existing file
- [Django]-You are trying to add a non-nullable field 'new_field' to userprofile without a default
- [Django]-Running a specific test case in Django when your app has a tests directory
0👍
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
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Django – view sql query without publishing migrations
- [Django]-Django 2, python 3.4 cannot decode urlsafe_base64_decode(uidb64)