Django Many-to-many relationships Simple Many To Many Relationship.

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Django Question?