Bird
Raised Fist0
Djangoframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which Django field lookup would you use to find records where a field exactly matches a given value?
easy
A. exact
B. contains
C. gt
D. lt

Solution

  1. Step 1: Understand the purpose of each lookup

    The exact lookup matches fields that are exactly equal to the given value. contains checks for substring presence, gt means greater than, and lt means less than.
  2. Step 2: Match the requirement to the lookup

    Since the question asks for exact matches, exact is the correct lookup.
  3. Final Answer:

    exact -> Option A
  4. Quick Check:

    Exact match = exact [OK]
Hint: Exact match uses 'exact' lookup in Django queries [OK]
Common Mistakes:
  • Confusing 'contains' with exact match
  • Using 'gt' or 'lt' for equality checks
  • Assuming 'exact' is default without specifying
2. Which of the following is the correct syntax to filter a Django model Book for titles containing the word 'django'?
easy
A. Book.objects.filter(title__exact='django')
B. Book.objects.filter(title__gt='django')
C. Book.objects.filter(title__contains='django')
D. Book.objects.filter(title__lt='django')

Solution

  1. Step 1: Identify the lookup for substring matching

    The contains lookup is used to find records where the field contains the given substring anywhere inside it.
  2. Step 2: Check the syntax for filtering

    The correct syntax uses double underscores to specify the lookup: title__contains='django'.
  3. Final Answer:

    Book.objects.filter(title__contains='django') -> Option C
  4. Quick Check:

    Substring search = contains [OK]
Hint: Use __contains for substring filters in Django queries [OK]
Common Mistakes:
  • Using __exact for substring search
  • Using __gt or __lt for string matching
  • Missing double underscores in lookup
3. Given the model Product with a field price, what will this query return?
Product.objects.filter(price__gt=100)
medium
A. All products with price greater than 100
B. All products with price exactly 100
C. All products with price less than 100
D. All products with price containing '100'

Solution

  1. Step 1: Understand the lookup used

    The lookup price__gt=100 means filter products where the price is greater than 100.
  2. Step 2: Interpret the query result

    The query returns all products with price values strictly greater than 100, excluding 100 itself.
  3. Final Answer:

    All products with price greater than 100 -> Option A
  4. Quick Check:

    gt means greater than [OK]
Hint: gt means greater than in Django filters [OK]
Common Mistakes:
  • Confusing gt with exact or contains
  • Thinking gt includes the value 100
  • Assuming it filters less than 100
4. You wrote this Django query but it raises an error:
Entry.objects.filter(date__gt='2023-01-01')

What is the most likely cause?
medium
A. You must use 'exact' lookup for date filtering
B. The field 'date' is not a DateField or DateTimeField
C. The date string format is incorrect for filtering
D. The lookup 'gt' is not valid for date fields

Solution

  1. Step 1: Check field type compatibility

    The gt lookup works with fields that support ordering like DateField or DateTimeField. If date is not one of these, the query will error.
  2. Step 2: Validate other options

    The lookup gt is valid for date fields, and the string format is acceptable for Django's date parsing. Using exact is not required.
  3. Final Answer:

    The field 'date' is not a DateField or DateTimeField -> Option B
  4. Quick Check:

    gt requires comparable field type [OK]
Hint: Ensure field type supports lookup before filtering [OK]
Common Mistakes:
  • Assuming all fields support gt lookup
  • Incorrect date string format causing error
  • Using exact lookup unnecessarily
5. You want to find all Order records where the status field contains 'pending' (case insensitive) and the total is less than 500. Which Django query correctly applies these filters?
hard
A. Order.objects.filter(status__contains='pending', total__gt=500)
B. Order.objects.filter(status__contains='pending', total__lt=500)
C. Order.objects.filter(status__exact='pending', total__lt=500)
D. Order.objects.filter(status__icontains='pending', total__lt=500)

Solution

  1. Step 1: Choose case-insensitive substring lookup

    To find 'pending' regardless of case, use icontains instead of contains.
  2. Step 2: Apply less than filter on total

    The total__lt=500 filters orders with total less than 500.
  3. Step 3: Combine filters correctly

    Both filters are passed as keyword arguments to filter() to apply AND logic.
  4. Final Answer:

    Order.objects.filter(status__icontains='pending', total__lt=500) -> Option D
  5. Quick Check:

    Case-insensitive contains + less than = icontains + lt [OK]
Hint: Use icontains for case-insensitive substring filters [OK]
Common Mistakes:
  • Using contains instead of icontains for case insensitivity
  • Mixing lt and gt incorrectly
  • Using exact instead of contains for substring search