0
0
Djangoframework~10 mins

F expressions for field comparisons in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - F expressions for field comparisons
Start Query
Use F expression
Compare model fields
Generate SQL with field references
Execute query
Return filtered results
This flow shows how Django uses F expressions to compare fields within the same model in a query.
Execution Sample
Django
from django.db.models import F

# Filter products where price > cost
Product.objects.filter(price__gt=F('cost'))
This code filters Product records where the price field is greater than the cost field using an F expression.
Execution Table
StepActionF Expression EvaluationSQL GeneratedQuery Result
1Start filter queryN/ASELECT * FROM productNo results yet
2Apply filter price__gt=F('cost')Compare price and cost fieldsSELECT * FROM product WHERE price > costQuery prepared
3Execute queryDatabase compares price and cost per rowN/AReturns products with price > cost
4EndN/AN/AFiltered queryset returned
💡 Query completes after filtering all rows where price is greater than cost
Variable Tracker
VariableStartAfter Step 2After Step 3Final
querysetAll productsFiltered by price > costExecuted queryFiltered products returned
Key Moments - 2 Insights
Why do we use F('cost') instead of just 'cost' in the filter?
Using F('cost') tells Django to compare the database field 'cost' directly, not a fixed value. See execution_table step 2 where the SQL uses price > cost.
Does the comparison happen in Python or in the database?
The comparison happens in the database as shown in execution_table step 3, where the SQL query compares fields for each row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what SQL is generated at step 2?
ASELECT * FROM product WHERE price > 100
BSELECT * FROM product WHERE price > cost
CSELECT * FROM product WHERE price > 'cost'
DSELECT * FROM product
💡 Hint
Check the 'SQL Generated' column at step 2 in the execution_table
At which step does the database compare the fields price and cost?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Query Result' columns in execution_table step 3
If we replaced F('cost') with a fixed value 50, how would the SQL change at step 2?
ASELECT * FROM product WHERE price > 'cost'
BSELECT * FROM product WHERE price > cost
CSELECT * FROM product WHERE price > 50
DSELECT * FROM product
💡 Hint
Think about how fixed values appear in SQL compared to field references in execution_table step 2
Concept Snapshot
F expressions let you compare model fields directly in queries.
Use F('fieldname') inside filters to refer to another field.
Django generates SQL comparing columns, not fixed values.
This runs in the database for efficiency.
Example: filter(price__gt=F('cost')) finds rows where price > cost.
Full Transcript
This visual trace shows how Django's F expressions work for comparing fields in a query. First, the query starts with all products. Then, using filter(price__gt=F('cost')), Django prepares SQL that compares the price and cost columns directly. The database executes this SQL, returning only products where price is greater than cost. The key is that F('cost') tells Django to use the database field, not a fixed value. This makes the comparison efficient and dynamic. The variable 'queryset' changes from all products to the filtered set after the query runs. Understanding this flow helps beginners see how Django builds and runs queries with field comparisons.