Challenge - 5 Problems
Queryset Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this queryset slicing?
Given a Django model
Book with 10 entries ordered by published_date, what does Book.objects.order_by('published_date')[2:5] return?Attempts:
2 left
💡 Hint
Remember Python slicing starts at index 0 and excludes the stop index.
✗ Incorrect
The slice [2:5] selects items starting at index 2 up to but not including index 5. So it returns the 3rd, 4th, and 5th books in the ordered queryset.
📝 Syntax
intermediate2:00remaining
Which queryset ordering syntax is correct?
Select the correct way to order a queryset of
Author by last name descending and then first name ascending.Attempts:
2 left
💡 Hint
Use a minus sign before the field name for descending order.
✗ Incorrect
In Django, prefixing a field name with - orders descending. So order_by('-last_name', 'first_name') orders by last name descending, then first name ascending.
🔧 Debug
advanced2:00remaining
Why does this queryset slicing cause an error?
Consider this code:
books = Book.objects.order_by('title')[5:2]. What happens when this runs?Attempts:
2 left
💡 Hint
Think about how Python slicing behaves when start index is larger than stop index.
✗ Incorrect
Python slicing with start index greater than stop index returns an empty list or queryset. It does not raise an error.
🧠 Conceptual
advanced2:00remaining
How does chaining order_by calls affect the queryset?
What is the result of this queryset:
Book.objects.order_by('author').order_by('published_date')?Attempts:
2 left
💡 Hint
Each order_by call overrides the previous ordering.
✗ Incorrect
In Django, calling order_by multiple times replaces the previous ordering. Only the last order_by call applies.
❓ state_output
expert2:00remaining
What is the number of items in this sliced queryset?
If
Entry.objects.all() returns 15 items, how many items does Entry.objects.order_by('-created_at')[3:10] contain?Attempts:
2 left
💡 Hint
Count the items between indices 3 and 9 inclusive.
✗ Incorrect
The slice [3:10] includes items at indices 3 through 9, which is 7 items total.