views:

101

answers:

0

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()