related_name and Reverse Lookups
Traverse relationships in both directions.
related_name and Reverse Lookups is a free Django Academy lesson on CoddyKit — lesson 4 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.
Links Go Both Ways
You set a ForeignKey from book to author, but you can also walk it backward. A reverse lookup reaches the many side from the one side.
The Default _set Manager
By default Django names the reverse accessor modelname_set. From an author, the books appear as book_set.
author = Author.objects.first()
author.book_set.all()_set Is a Manager
That book_set is a full manager, so you can chain filter, order_by, and count on it just like any query.
author.book_set.filter(published=True).count()Naming with related_name
The _set name is clunky. Set related_name on the ForeignKey to pick a clearer reverse accessor yourself.
author = models.ForeignKey(Author, related_name="books", on_delete=models.CASCADE)A Cleaner Reverse
Now the reverse reads naturally. Instead of book_set you simply write books, which is far easier to follow.
author.books.all()Reverse in Queries
related_name also drives filter across the link. Use the chosen name with the double underscore from the other model.
Author.objects.filter(books__published=True)The Default Query Name
If you skip related_name, the reverse query name is the lowercase model name, like author.objects.filter(book__title=...).
Avoiding Name Clashes
Two ForeignKeys from one model to the same target collide on the reverse name. A distinct related_name on each fixes it.
writer = models.ForeignKey(User, related_name="written", on_delete=models.CASCADE)
editor = models.ForeignKey(User, related_name="edited", on_delete=models.CASCADE)Turning Off the Reverse
Set related_name to a value ending in + to disable the reverse accessor entirely when you never need it.
owner = models.ForeignKey(User, related_name="+", on_delete=models.CASCADE)related_query_name
You can split the names: related_query_name sets the filter name separately from the attribute used to read related rows.
tags = models.ManyToManyField(Tag, related_name="posts", related_query_name="post")Works for Every Relation
related_name applies to ForeignKey, ManyToManyField, and OneToOneField alike, so every reverse path can read clearly.
Quick Check
You set related_name="books" on Book's author ForeignKey. How do you list an author's books?
Recap
You did it! Reverse lookups walk a link backward, and related_name turns clunky _set accessors into clear, readable names. 🔄
Frequently asked questions
Is the “related_name and Reverse Lookups” lesson free?
Yes — the full text of “related_name and Reverse Lookups” 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 “related_name and Reverse Lookups”?
Traverse relationships in both directions. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “related_name and Reverse Lookups” 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
- ForeignKey and on_delete
- ManyToManyField and Through Models
- OneToOneField for Profiles
- related_name and Reverse Lookups