Complete the code to create a queryset that fetches all objects from the model.
all_items = Model.objects.[1]()The all() method returns a queryset of all objects in the model. It does not hit the database immediately, making it lazy.
Complete the code to filter objects where 'status' is 'active'.
active_items = Model.objects.[1](status='active')
The filter() method returns a lazy queryset with objects matching the condition. It does not query the database until needed.
Fix the error in the code to get the first object or None without hitting the database multiple times.
first_item = Model.objects.[1]().first()first() to get a single object.The all() method returns a queryset, and calling first() on it fetches the first object or None efficiently. Using get() would raise errors if multiple objects exist.
Fill both blanks to create a queryset that orders items by 'created' date descending and limits to 5 results.
recent_items = Model.objects.[1]('-created')[:[2]]
The order_by('-created') sorts the queryset by 'created' date descending. Using slicing [:5] limits the results to 5. Both together create a lazy, efficient query.
Fill all three blanks to create a dictionary comprehension that maps usernames to emails for users active and joined after 2020.
user_dict = {user.[1]: user.[2] for user in User.objects.[3](is_active=True, joined__year__gt=2020)}This comprehension creates a dictionary with usernames as keys and emails as values. The queryset uses filter() to select active users who joined after 2020. This is lazy and efficient.