0
0
Djangoframework~20 mins

F expressions for field comparisons in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
F Expressions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django query return?
Consider a Django model Product with fields price and discount_price. What does this query return?
Product.objects.filter(price__gt=F('discount_price'))
AAll products where the price is greater than the discount price.
BAll products where the price is less than the discount price.
CAll products where the price equals the discount price.
DAll products regardless of price or discount price.
Attempts:
2 left
💡 Hint
Remember that F('discount_price') refers to the value of the discount_price field in the database.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses an F expression to find records where two fields are equal?
You want to find all Order objects where total_price equals amount_paid. Which query is correct?
AOrder.objects.filter(total_price=F('amount_paid'))
BOrder.objects.filter(total_price__eq=F('amount_paid'))
COrder.objects.filter(F('total_price') == F('amount_paid'))
DOrder.objects.filter(total_price=amount_paid)
Attempts:
2 left
💡 Hint
Use the correct Django ORM syntax for field comparisons with F expressions.
🔧 Debug
advanced
2:00remaining
Why does this Django query raise an error?
Given the model Employee with fields salary and bonus, what causes the error in this query?
Employee.objects.filter(salary > F('bonus'))
AF expressions cannot be used with numeric fields.
BThe field names salary and bonus must be strings inside F expressions.
CThe filter method requires a list, not a comparison.
DThe comparison operator > cannot be used directly inside filter; use lookup syntax instead.
Attempts:
2 left
💡 Hint
Check how Django expects comparisons in filter arguments.
state_output
advanced
2:00remaining
What is the result count of this query?
Assume the Book model has fields pages and read_pages. Which query returns books where pages are at least twice the read_pages?
Book.objects.filter(pages__gte=F('read_pages') * 2)

What does this query return?
ABooks where pages equal read_pages multiplied by 2.
BBooks where the read pages are greater than or equal to twice the total pages.
CBooks where the total pages are greater than or equal to twice the read pages.
DBooks where pages are less than twice the read pages.
Attempts:
2 left
💡 Hint
Remember that __gte means greater than or equal to.
🧠 Conceptual
expert
3:00remaining
What happens when you use F expressions in update() with field comparisons?
Consider this code:
Product.objects.filter(stock__lt=F('reorder_level')).update(stock=F('stock') + 10)

What is the effect of this update?
AIt decreases stock by 10 for products where stock is less than reorder_level.
BIt increases the stock by 10 only for products where stock is less than reorder_level.
CIt sets stock to 10 for all products regardless of stock or reorder_level.
DIt causes a runtime error because F expressions cannot be used in update().
Attempts:
2 left
💡 Hint
Think about how update() and F expressions work together in Django.