开发者

How to find objects with a join in common?

开发者 https://www.devze.com 2022-12-11 13:03 出处:网络
How do I find all th开发者_运维技巧e Members that bob shares a group with? class Member(Model):

How do I find all th开发者_运维技巧e Members that bob shares a group with?

class Member(Model):
     name = CharField(max_length=30)

class GroupMember(Model):
     member = ForeignKey(Member)
     group  = ForeignKey(Group)

class Group(Model):
     name = CharField(max_length=30)


Member.objects.filter(group__in=bob.group_set.all()).exclude(pk=bob.pk)

Edit I didn't notice that you didn't have a ManyToMany relationship set up between Member and Group. You'll need to add that:

class Group(Model):
   name = CharField(max_length=30)
   members = ManyToManyField(Member, through='Membership')

now syncdb and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消