0Pricing
Django Academy · Lesson

ManyToManyField and Through Models

Connect records both ways with extra data.

ManyToManyField and Through Models is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

The Hidden Join Table

Behind the scenes Django builds a join table holding pairs of ids. No new column lands on either of your two models.

Adding Relations

Once a post is saved, attach tags with add. You can pass several objects at once in a single call.

post.tags.add(python_tag, web_tag)

Removing and Clearing

Drop a single link with remove, or wipe them all with clear. The related objects themselves stay safe.

post.tags.remove(web_tag)
post.tags.clear()

Reading the Set

Access linked rows through the field like a normal manager. Call all to loop over everything connected.

for tag in post.tags.all():
    print(tag.name)

When the Link Needs Data

What if a membership has its own facts, like a join date? A plain join table cannot hold extra columns, so you need more control.

The through Model

A through model is your own table for the relationship. It lets each link carry extra fields you define.

members = models.ManyToManyField(User, through="Membership")

Defining the through Class

The through model holds a ForeignKey to each side plus any extra fields. Those columns describe the connection itself.

class Membership(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    joined = models.DateField()

add Stops Working

With a custom through model, plain add is disabled. You create the link by making a through row directly instead.

Membership.objects.create(user=u, group=g, joined=today)

Querying Across M2M

Filter through a many-to-many link with the double underscore, just like a ForeignKey. The join table is handled for you.

Post.objects.filter(tags__name="python")

Quick Check

Your group membership needs to record when each user joined. What do you reach for?

Recap

Well done! A ManyToManyField links both sides freely, and a through model lets each connection carry its own data. 🏷️

Frequently asked questions

Is the “ManyToManyField and Through Models” lesson free?

Yes — the full text of “ManyToManyField and Through Models” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “ManyToManyField and Through Models”?

Connect records both ways with extra data. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ManyToManyField and Through Models” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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