Complete the code to define a ForeignKey with a related name for reverse access.
class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name=[1])
The related_name defines the name to access all books from an author instance, so "books" is a clear and common choice.
Complete the code to access all books of an author using the related name.
author = Author.objects.get(id=1) books = author.[1].all()
book_set when a related_name is set.You use the related_name defined in the ForeignKey to access related objects. Here, books is the correct related name.
Fix the error in the ForeignKey definition to correctly set the related name.
class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name=[1])
The related_name should be plural to represent multiple comments related to a post, so "comments" is correct.
Fill both blanks to define a ForeignKey with a related name and access the related objects.
class Entry(models.Model): blog = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name=[1]) entries = blog.[2].all()
entry_set when related_name is set.The related_name is set to "entries", so accessing related objects uses the same name.
Fill all three blanks to define a ForeignKey with a related name, access related objects, and filter them.
class Review(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name=[1]) reviews = product.[2].filter(rating__[3]=5)
The related_name is "reviews", so you access product.reviews. The filter uses rating__gte=5 to get reviews with rating 5 or higher.