0
0
Djangoframework~10 mins

Field lookups (exact, contains, gt, lt) in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Field lookups (exact, contains, gt, lt)
Start Query
Choose Field Lookup
Apply Filter Condition
Database Filters Rows
Return Filtered Results
End
This flow shows how Django applies field lookups to filter database records step-by-step.
Execution Sample
Django
Book.objects.filter(title__exact='Django')
Book.objects.filter(title__contains='Django')
Book.objects.filter(pages__gt=100)
Book.objects.filter(pages__lt=50)
These queries filter books by exact title, title containing text, pages greater than, and pages less than values.
Execution Table
StepQueryLookup TypeCondition EvaluatedRows MatchedResult
1title__exact='Django'exacttitle == 'Django'2Returns 2 books with title exactly 'Django'
2title__contains='Django'contains'Django' in title3Returns 3 books with 'Django' in title
3pages__gt=100gtpages > 1004Returns 4 books with pages greater than 100
4pages__lt=50ltpages < 501Returns 1 book with pages less than 50
5End---No more queries
💡 All queries processed and filtered results returned.
Variable Tracker
VariableStartAfter Query 1After Query 2After Query 3After Query 4Final
filtered_books[][Book1, Book2][Book1, Book2, Book3][Book4, Book5, Book6, Book7][Book8]Final filtered sets per query
Key Moments - 3 Insights
Why does 'title__exact' only match exact titles and not partial words?
Because 'exact' lookup checks for complete equality as shown in execution_table row 1, it does not match substrings.
How does 'contains' differ from 'exact' in filtering?
'contains' checks if the substring exists anywhere in the field, as seen in execution_table row 2, so it matches more results.
What happens if you use 'gt' or 'lt' on a non-numeric field?
Django will raise an error because 'gt' and 'lt' expect comparable numeric or date fields, not strings.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many books matched the 'pages__gt=100' query?
A2
B3
C4
D1
💡 Hint
Check the 'Rows Matched' column in row 3 of the execution_table.
At which step does the query check if the title contains 'Django'?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for 'contains' in the 'Lookup Type' column in the execution_table.
If you change 'pages__lt=50' to 'pages__lt=150', what happens to matched rows?
AFewer rows will match
BMore rows will match
CSame number of rows match
DQuery will fail
💡 Hint
Refer to how 'lt' compares values in the execution_table and variable_tracker.
Concept Snapshot
Django field lookups filter querysets by conditions.
Use __exact for exact match.
Use __contains for substring match.
Use __gt and __lt for greater or less than comparisons.
Filters return matching database rows.
Syntax: Model.objects.filter(field__lookup=value).
Full Transcript
This visual execution trace shows how Django field lookups work step-by-step. Starting a query, Django applies the chosen lookup like exact, contains, gt, or lt to filter database rows. For example, title__exact='Django' matches only titles exactly equal to 'Django'. Title__contains='Django' matches any title containing that word. Pages__gt=100 matches books with more than 100 pages. Pages__lt=50 matches books with fewer than 50 pages. The execution table tracks each query step, condition checked, rows matched, and results returned. The variable tracker shows how filtered results change after each query. Key moments clarify common confusions like exact vs contains behavior and numeric comparisons. The quiz tests understanding by referencing the execution table and variable states. This helps beginners see how Django filters data visually and clearly.