[Answer]-Queryset for ManyToMany relation in Django

1๐Ÿ‘

โœ…

I just create your base. Create group, user and connect them:

u = User.objects.create(name='user')
g = Group.objects.create()
m = Member.objects.create(group=g, user=u)

After this i can get user groups like this:

u.Member_list.all()
# output: [<Group: Group object>]

My base:

class User(models.Model):
    name = models.CharField(max_length=4)


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)
๐Ÿ‘คzymud

Leave a comment