Django many-to-many relationship to self with extra data, how do I select from a certain direction?

Posted by Jake on Stack Overflow See other posts from Stack Overflow or by Jake
Published on 2010-03-08T07:02:01Z Indexed on 2010/03/08 7:06 UTC
Read the original article Hit count: 261

I have some hierarchical data where each Set can have many members and can belong to more than one Set(group)

Here are the models:

class Set(models.Model):
    ...
    groups = models.ManyToManyField('self', through='Membership', symmetrical=False)
    members = models.ManyToManyField('self', through='Membership', symmetrical=False)

class Membership(models.Model):
    group = models.ForeignKey( Set, related_name='Members' )
    member = models.ForeignKey( Set, related_name='Groups' )
    order = models.IntegerField( default=-1 )

I want to know how to get all the members or all the groups for a Set instance. I think I can do it as follows, but it's not very logical, can anyone tell me what's going on and how I should be doing it?

# This gives me a set of Sets
# Which seems to be the groups this Set belongs to
set_instance.set_set.all()

# These give me a set of Memberships, not Sets
set_instance.Members.all()
set_instance.Groups.all()

# These they both return a set of Sets
# which seem to be the members of this one
set_instance.members.all()
set_instance.groups.all()

© Stack Overflow or respective owner

Related posts about django

Related posts about many-to-many