0
0
Djangoframework~10 mins

Relationship query patterns 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 get all books written by an author with id 1.

Django
books = Book.objects.filter(author__[1]=1)
Drag options to blanks, or click blank then click option'
Aid
Bname
Ctitle
Dpk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'id' causes no results if filtering by author id.
Using 'title' is wrong because it's a Book field, not Author.
2fill in blank
medium

Complete the code to get all authors who have written a book titled 'Django Basics'.

Django
authors = Author.objects.filter(book__[1]='Django Basics')
Drag options to blanks, or click blank then click option'
Aname
Bid
Cauthor
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' filters by author name, not book title.
Using 'author' is invalid because it's a reverse relation here.
3fill in blank
hard

Fix the error in the code to get all books with at least one review rating greater than 4.

Django
books = Book.objects.filter(review__[1]__gt=4)
Drag options to blanks, or click blank then click option'
Arating
Bscore
Crate
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'score' or 'value' causes errors if those fields don't exist.
Using 'rate' is incorrect if the model field is named 'rating'.
4fill in blank
hard

Fill both blanks to get all authors who have written more than 3 books.

Django
authors = Author.objects.annotate(book_count=[1]).filter(book_count__[2]=3)
Drag options to blanks, or click blank then click option'
ACount('book')
BSum('book')
Cgt
Dlt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Sum' instead of 'Count' aggregates incorrectly.
Using 'lt' filters authors with fewer than 3 books, opposite of requirement.
5fill in blank
hard

Fill all three blanks to get all books with reviews rated at least 4 and order them by author's name.

Django
books = Book.objects.filter(review__[1]__[2]=4).order_by('[3]')
Drag options to blanks, or click blank then click option'
Arating
Bgte
Cauthor__name
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'gt' instead of 'gte' excludes reviews rated exactly 4.
Ordering by 'title' orders by book title, not author's name.