tags:

views:

46

answers:

1

Given a specific member (eg: John) how do I find all the Members that John shares a group with?

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

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

class Group(Model):
     name = CharField(max_length=30)
A: 
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.

Daniel Roseman
AttributeError: 'Member' object has no attribute 'group_set'
John Mee
Updated answer.
Daniel Roseman