Challenge - 5 Problems
Django Search & Ordering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django queryset ordering?
Given the model
Book with fields title and published_date, what will be the order of titles returned by this queryset?Book.objects.order_by('-published_date', 'title').values_list('title', flat=True)Django
Assume the database has these books:<br>- 'Alpha' published 2020-01-01<br>- 'Beta' published 2021-01-01<br>- 'Gamma' published 2021-01-01
Attempts:
2 left
💡 Hint
Ordering first by published_date descending, then by title ascending.
✗ Incorrect
The queryset orders books first by published_date descending, so 2021 books come first. Among those with the same date, titles are ordered ascending alphabetically. So 'Beta' comes before 'Gamma'.
📝 Syntax
intermediate2:00remaining
Which option correctly filters and orders a queryset by a search term in Django?
You want to find all
Product objects whose name contains 'phone' (case-insensitive) and order them by price ascending. Which queryset is correct?Attempts:
2 left
💡 Hint
Use case-insensitive contains and ascending order.
✗ Incorrect
Option C uses
icontains for case-insensitive search and orders by price ascending. Others either use case-sensitive filter or wrong order direction.🔧 Debug
advanced2:00remaining
Why does this Django queryset raise a FieldError?
Consider this queryset:
Given
Entry.objects.order_by('author__name')Given
Entry has a ForeignKey to Author model, why does this raise FieldError?Attempts:
2 left
💡 Hint
Ordering by related fields needs proper query setup.
✗ Incorrect
Ordering by a related field like 'author__name' requires Django to join the related table. Without select_related(), Django may raise FieldError if the join is missing or ambiguous.
🧠 Conceptual
advanced2:00remaining
What is the effect of using distinct() after order_by() in a Django queryset?
Given a queryset:
What does this do?
MyModel.objects.order_by('category').distinct('category')What does this do?
Attempts:
2 left
💡 Hint
distinct() with fields returns unique rows by those fields.
✗ Incorrect
Using distinct('category') returns one record per unique category, ordered by category ascending. However, this is only supported on PostgreSQL and may raise errors on other databases.
❓ state_output
expert3:00remaining
What is the output of this Django search and ordering code snippet?
Given the model
- 'Django Tips', views=100
- 'Python Tricks', views=150
- 'Django Advanced', views=100
What does this code output?
Article with fields title and views, and these records:- 'Django Tips', views=100
- 'Python Tricks', views=150
- 'Django Advanced', views=100
What does this code output?
qs = Article.objects.filter(title__icontains='django').order_by('-views', 'title').values_list('title', flat=True)
list(qs)Attempts:
2 left
💡 Hint
Filter by 'django' in title, order by views descending, then title ascending.
✗ Incorrect
Filter keeps 'Django Tips' and 'Django Advanced'. Both have 100 views, so ordering by -views keeps them equal, then title ascending orders 'Django Advanced' before 'Django Tips'.