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.
精彩评论