Django Simple Many To Many Relationship.


Example

class Person(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()

class Club(models.Model):
    name = models.CharField(max_length=50)
    members = models.ManyToManyField(Person)

Here we define a relationship where a club has many Persons and members and a Person can be a member of several different Clubs.

Though we define only two models, django actually creates three tables in the database for us. These are myapp_person, myapp_club and myapp_club_members. Django automatically creates a unique index on myapp_club_members(club_id,person_id) columns.