related_name and Reverse Lookups
Traverse relationships in both directions.
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()All lessons in this course
- ForeignKey and on_delete
- ManyToManyField and Through Models
- OneToOneField for Profiles
- related_name and Reverse Lookups