0
0
Djangoframework~20 mins

Search and ordering in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Search & Ordering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
A['Beta', 'Gamma', 'Alpha']
B['Gamma', 'Beta', 'Alpha']
C['Alpha', 'Beta', 'Gamma']
D['Alpha', 'Gamma', 'Beta']
Attempts:
2 left
💡 Hint
Ordering first by published_date descending, then by title ascending.
📝 Syntax
intermediate
2: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?
AProduct.objects.filter(name__icontains='phone').order_by('-price')
BProduct.objects.filter(name__contains='phone').order_by('-price')
CProduct.objects.filter(name__icontains='phone').order_by('price')
DProduct.objects.filter(name__contains='phone').order_by('price')
Attempts:
2 left
💡 Hint
Use case-insensitive contains and ascending order.
🔧 Debug
advanced
2:00remaining
Why does this Django queryset raise a FieldError?
Consider this queryset:
Entry.objects.order_by('author__name')
Given Entry has a ForeignKey to Author model, why does this raise FieldError?
ABecause ordering by related fields requires the related field to be in Meta.ordering
BBecause ForeignKey fields cannot be used in order_by
CBecause 'author__name' is a valid field and should not raise an error
DBecause 'author__name' is not a valid field for ordering without using select_related()
Attempts:
2 left
💡 Hint
Ordering by related fields needs proper query setup.
🧠 Conceptual
advanced
2:00remaining
What is the effect of using distinct() after order_by() in a Django queryset?
Given a queryset:
MyModel.objects.order_by('category').distinct('category')
What does this do?
AReturns all records ordered by category, ignoring duplicates
BReturns one unique record per category, ordered by category ascending
CRaises a NotSupportedError because distinct with fields is not supported on all databases
DReturns records ordered by category but distinct() has no effect
Attempts:
2 left
💡 Hint
distinct() with fields returns unique rows by those fields.
state_output
expert
3:00remaining
What is the output of this Django search and ordering code snippet?
Given the model 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)
A['Django Advanced', 'Django Tips']
B['Django Tips', 'Django Advanced']
C['Python Tricks', 'Django Tips', 'Django Advanced']
D['Django Tips']
Attempts:
2 left
💡 Hint
Filter by 'django' in title, order by views descending, then title ascending.