On Wed, Mar 25, 2009 at 9:54 PM, Bro <coolpari...@gmail.com> wrote: > > Hi, > > I'm trying to use ManyToMany in 2 sens : > > ------------------------------- > class Person(models.Model): > name = models.CharField(max_length=128) > groups = models.ManyToManyField(Group, through='Membership') > > def __unicode__(self): > return self.name > > class Group(models.Model): > name = models.CharField(max_length=128) > members = models.ManyToManyField(Person, through='Membership') > > def __unicode__(self): > return self.name > > class Membership(models.Model): > person = models.ForeignKey(Person) > group = models.ForeignKey(Group) > date_joined = models.DateField() > invite_reason = models.CharField(max_length=64) > ------------------------------- > > But when the Person class is created, the group class isn't and it > doesn't work. > Is it possible ?
Looks like you've just been a little overenthusiastic with your model definitions. You've got a ManyToMany field defined on both 'sides' of the relationship. As your models are defined, you actually have two independent relationships between Person and Group - Person's relationship with Group, and Group's relationship with Person. In Django, you only need to define one side of the ManyToMany relationship - the reverse direction is then implied from the model definition. You can use the 'related_name' attribute to control the name given to the implied reverse attribute. The docs go into a lot more detail on this: http://docs.djangoproject.com/en/dev/topics/db/queries/#many-to-many-relationships http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany The following model definitions should do what you're looking for: class Person(models.Model): name = models.CharField(max_length=128) groups = models.ManyToManyField(Group, through='Membership', related_name='members') class Group(models.Model): name = models.CharField(max_length=128) Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---