如何找到具有共同连接的对象?

时间:2009-11-10 06:03:27

标签: django

如何找到bob与之分享的所有会员?

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)

1 个答案:

答案 0 :(得分:1)

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

修改我没有注意到您在成员和群组之间没有设置ManyToMany关系。您需要添加:

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

现在syncdb,它应该工作。