Creating many-to-many relationships with an intermediary model

In previous chapters, you created many-to-many relationships by adding ManyToManyField to one of the related models and letting Django create the database table for the relationship. This is suitable for most of the cases, but sometimes you may need to create an intermediate model for the relation. Creating an intermediary model is necessary when you want to store additional information for the relationship, for example, the date when the relation was created, or a field that describes the nature of the relationship.

We will create an intermediary model to build relationships between users. There are two reasons why we want to use an intermediate model:

  • We are using the User ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.