Database relationships in Django: Part 3

Series: Database relationships in Django

  1. Database relationships in Django
  2. Database relationships in Django: Part 2
  3. Database relationships in Django: Part 3

Our final relationship type is many to many relationships, these are the most complex, yet flexible, type of relationship, since they require an intermediate database table, known as a through table. This table can track other data but by default it just two Foreign Keys to each side of the relationship.

Examples of a many to many include authors of books (some books do have multiple authors!), items in a supermarket or ingredients for a meal. To go back to our English sentence it is as follows:

"A Meal can be made of many ingredients, and an ingredient can be part of many meals"

Django does make this easy with the ManyToManyField which by default handles the creation of the through table, although you can specify one if you need, and the ORM has a nice API to jump over the through table to the other side of the relationship.

Tomorrow, we move on to forms!