Challenge - 5 Problems
Django Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does
Model.objects.all() return?In Django, when you call
Model.objects.all(), what type of object do you get back?Attempts:
2 left
💡 Hint
Think about what Django returns to let you work with multiple records easily.
✗ Incorrect
Model.objects.all() returns a QuerySet that contains all records of that model. It is lazy and can be further filtered or iterated.
❓ component_behavior
intermediate1:30remaining
What does
Model.objects.filter(field=value) return?In Django, what does
Model.objects.filter(field=value) return?Attempts:
2 left
💡 Hint
Remember, filter narrows down records but still returns multiple if found.
✗ Incorrect
filter() returns a QuerySet of all records matching the condition. It can be empty if no matches.
❓ state_output
advanced2:00remaining
What is the output of this Django QuerySet code?
Given the following Django model and code, what is the output of
print(qs.count())?Django
from django.db import models class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=6, decimal_places=2) # Assume database has 3 products: # Product(name='Pen', price=1.50), Product(name='Book', price=10.00), Product(name='Notebook', price=5.00) qs = Product.objects.filter(price__gt=5.00) print(qs.count())
Attempts:
2 left
💡 Hint
Look at which products have price greater than 5.00.
✗ Incorrect
Only 'Book' (10.00) and 'Notebook' (5.00) are checked. Since price__gt means strictly greater than 5.00, 'Notebook' is excluded. So only 'Book' counts.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error in Django QuerySet filtering?
Which of the following Django QuerySet filter calls will cause a syntax error?
Attempts:
2 left
💡 Hint
Check how keyword arguments are passed in Python functions.
✗ Incorrect
Option C uses quotes around the keyword argument which is invalid syntax in Python function calls.
🔧 Debug
expert2:30remaining
Why does this Django QuerySet filter return no results?
Given this code, why does
qs return an empty QuerySet?Django
qs = Product.objects.filter(name__startswith='Pen', price__lt=1.00)
Attempts:
2 left
💡 Hint
Think about the conditions combined with AND logic in filter.
✗ Incorrect
Filters combine conditions with AND logic. If no product matches both conditions simultaneously, the QuerySet is empty.