[Solved]-Django many-to-many relations, and through


You have to manage it yourself:

class MyGroup(models.Model):
    name = models.CharField(max_length=100)

class Membership(models.Model):
    group = models.ForeignKey(MyGroup)
    member = models.ForeignKey(User)

    invited_by = models.ForeignKey(User, related_name='invited_set')

Then instead of group.members.all() you do group.membership_set.all().

Also, I wouldn’t use ‘Group’ as your model name, as Django already has a Group object.


It is possible if you are using Django 1.7.

From the docs: https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships

In Django 1.6 and earlier, intermediate models containing more than
one foreign key to any of the models involved in the many-to-many
relationship used to be prohibited.

Leave a comment