0
0
Djangoframework~20 mins

Prefetch_related for reverse relations in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does prefetch_related do with reverse relations?

Consider two Django models: Author and Book, where Book has a foreign key to Author. If you use Author.objects.prefetch_related('book_set'), what is the main effect?

AIt does not affect the query and fetches authors without related books.
BIt joins the author and book tables in one query using SQL JOIN.
CIt fetches only the first book for each author to optimize performance.
DIt fetches all related books for each author in a single additional query, reducing database hits.
Attempts:
2 left
💡 Hint

Think about how Django avoids querying the database multiple times for related objects.

state_output
intermediate
2:00remaining
How many queries are executed with prefetch_related on reverse relation?

Given the models Author and Book (with Book.author as ForeignKey), what is the number of database queries executed by this code?

authors = Author.objects.prefetch_related('book_set').all()
for author in authors:
    print(len(author.book_set.all()))
ANo queries: data is fetched lazily when printing.
B1 query: a single JOIN query fetching authors and books.
C2 queries: one for authors and one for all related books.
DN+1 queries: one for authors and one per author for books.
Attempts:
2 left
💡 Hint

Remember how prefetch_related works internally with separate queries.

🔧 Debug
advanced
2:00remaining
Why does prefetch_related('books') raise an error on reverse relation?

Given models Author and Book where Book.author is a ForeignKey, what is wrong with this query?

Author.objects.prefetch_related('books').all()

Assuming no related_name is set on Book.author.

Aprefetch_related cannot be used on reverse relations without related_name.
BThe related name 'books' does not exist; the default reverse relation is 'book_set'.
CThe query will run but return empty related objects.
DThe query causes a syntax error due to missing parentheses.
Attempts:
2 left
💡 Hint

Check the default reverse relation name Django uses when no related_name is set.

📝 Syntax
advanced
2:00remaining
Which prefetch_related syntax correctly fetches nested reverse relations?

Given models Author, Book, and Chapter where Book has ForeignKey to Author and Chapter has ForeignKey to Book, which syntax correctly prefetches all books and their chapters for authors?

AAuthor.objects.prefetch_related('book_set__chapter_set').all()
BAuthor.objects.prefetch_related('book_set.chapter_set').all()
CAuthor.objects.prefetch_related(['book_set', 'chapter_set']).all()
DAuthor.objects.prefetch_related('book_set').prefetch_related('chapter_set').all()
Attempts:
2 left
💡 Hint

Think about how to specify nested relations in prefetch_related.

🧠 Conceptual
expert
3:00remaining
Why use Prefetch object with custom queryset on reverse relation?

When prefetching a reverse relation in Django, why might you use Prefetch with a custom queryset instead of a simple string?

ATo filter or order the related objects before they are attached to the main objects.
BTo force Django to use SQL JOIN instead of separate queries.
CTo disable caching of related objects for memory optimization.
DTo automatically update related objects in the database.
Attempts:
2 left
💡 Hint

Consider why you might want to control which related objects are fetched.