Answer the question
In order to leave comments, you need to log in
How to correctly compose a query for ManyToMany in Django?
No matter how much I read the documentation, I can't work properly with ORM and ManyToMany. I'm trying to request a list of groups in which the user is located.
class User(AbstractUser):
...
class Group(models.Model):
...
Member_list = models.ManyToManyField(User, through='Member', related_name="Member_list")
class Member(models.Model):
group = models.ForeignKey(Group)
user = models.ForeignKey(User)
...
Answer the question
In order to leave comments, you need to log in
Hooray! Found a way to do this:
e = self.request.user
e.Member_list.all()
# output: [<Group: Group object>]
I don’t quite understand what you should have, but if the user should be in a group, then you need to
class User(AbstractUser):
groups = models.ManyToManyField(User, through='Group')
user = request.user
groups = user.groups.all()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question