0
0
Djangoframework~10 mins

Why querysets are lazy and powerful in Django - Test Your Understanding

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

Complete the code to create a queryset that fetches all objects from the model.

Django
all_items = Model.objects.[1]()
Drag options to blanks, or click blank then click option'
Aall
Bfilter
Cget
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'all' which returns a single object, not a queryset.
Using 'create' which inserts a new object instead of fetching.
2fill in blank
medium

Complete the code to filter objects where 'status' is 'active'.

Django
active_items = Model.objects.[1](status='active')
Drag options to blanks, or click blank then click option'
Aall
Bget
Cexclude
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns a single object and raises error if multiple found.
Using 'exclude' which returns objects not matching the condition.
3fill in blank
hard

Fix the error in the code to get the first object or None without hitting the database multiple times.

Django
first_item = Model.objects.[1]().first()
Drag options to blanks, or click blank then click option'
Aget
Bfilter
Call
Dexclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which raises exceptions if multiple or no objects found.
Using 'filter' without calling first() to get a single object.
4fill in blank
hard

Fill both blanks to create a queryset that orders items by 'created' date descending and limits to 5 results.

Django
recent_items = Model.objects.[1]('-created')[:[2]]
Drag options to blanks, or click blank then click option'
Aorder_by
Bfilter
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' instead of 'order_by' for sorting.
Using a wrong number for slicing.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps usernames to emails for users active and joined after 2020.

Django
user_dict = {user.[1]: user.[2] for user in User.objects.[3](is_active=True, joined__year__gt=2020)}
Drag options to blanks, or click blank then click option'
Ausername
Bemail
Cfilter
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'all' instead of 'filter' which does not apply conditions.
Swapping username and email in the dictionary keys and values.