0
0
Djangoframework~10 mins

Prefetch_related for reverse relations in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to prefetch the reverse relation 'books' for all authors.

Django
authors = Author.objects.all().[1]('books')
Drag options to blanks, or click blank then click option'
Afilter
Bprefetch_related
Cselect_related
Dannotate
Attempts:
3 left
💡 Hint
Common Mistakes
Using select_related instead of prefetch_related for reverse relations.
Forgetting to specify the related name 'books'.
2fill in blank
medium

Complete the code to prefetch the reverse relation 'comments' on a queryset of posts.

Django
posts = Post.objects.all().[1]('comments')
Drag options to blanks, or click blank then click option'
Aorder_by
Bannotate
Cprefetch_related
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using annotate which is for aggregation, not fetching related objects.
Using order_by which only sorts the queryset.
3fill in blank
hard

Fix the error in the code to correctly prefetch the reverse relation 'orders' for customers.

Django
customers = Customer.objects.all().[1]('order_set')
Drag options to blanks, or click blank then click option'
Aselect_related
Bexclude
Cfilter
Dprefetch_related
Attempts:
3 left
💡 Hint
Common Mistakes
Using select_related which only works for forward foreign keys.
Using incorrect related name instead of 'order_set'.
4fill in blank
hard

Fill both blanks to prefetch the reverse relation 'reviews' and filter only those with rating greater than 3.

Django
products = Product.objects.all().prefetch_related([1]('reviews', queryset=Review.objects.filter(rating [2] 3)))
Drag options to blanks, or click blank then click option'
APrefetch
B>
C<
Dselect_related
Attempts:
3 left
💡 Hint
Common Mistakes
Using select_related instead of Prefetch for custom queryset.
Using wrong comparison operator in filter.
5fill in blank
hard

Fill all three blanks to prefetch reverse relation 'entries' with a filtered queryset, and order the entries by 'date'.

Django
blogs = Blog.objects.all().prefetch_related([1]('entries', queryset=Entry.objects.filter(status=[2]).order_by([3])))
Drag options to blanks, or click blank then click option'
APrefetch
B'published'
C'date'
Dselect_related
Attempts:
3 left
💡 Hint
Common Mistakes
Using select_related instead of Prefetch.
Forgetting to quote string values in filter and order_by.