0Pricing
Django Academy · Lesson

ManyToManyField and Through Models

Connect records both ways with extra data.

Both Sides Have Many

Sometimes each side links to many of the other: a post has many tags, a tag marks many posts. That is a many-to-many relationship.

Declaring ManyToManyField

Add a ManyToManyField pointing at the other model. Django handles the link from both directions automatically.

class Post(models.Model):
    tags = models.ManyToManyField(Tag)

All lessons in this course

  1. ForeignKey and on_delete
  2. ManyToManyField and Through Models
  3. OneToOneField for Profiles
  4. related_name and Reverse Lookups
← Back to Django Academy