开发者

How to find objects with a join in common?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜