6👍
You don’t need to create a new attribute in Author
. You can get the books written by a specific author by following the ForeignKey
relationship backwards as follows:
author = Author.objects.get(name='George Orwell')
books = author.book_set.all()
The book_set
function is created by default by Django when you create the ForeignKey
relationship between Book
and Author
.
Source:stackexchange.com