Many To Many Relationship

Let’s learn how to make Many-to-Many relationships between Django models.

In the previous lesson, we saw how to make a Many-to-One relationship between two models. In this lesson, we will learn how to add a Many-to-Many relationship.

ManyToManyField field

This field is used for a Many-to-Many relationship. To understand how this relationship between models works, let’s add another model, Projects, to our current models from the previous lesson. Now we have these models:

  • Company
  • Employee
  • Projects

The new relationship we have added means that one project can have multiple employees, and one employee can work on multiple projects. This relationship is shown in the following illustration:

Get hands-on with 1200+ tech skills courses.