What if you could find exactly what you want in your data with just a simple, clear command?
Why Field lookups (exact, contains, gt, lt) in Django? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big list of books and you want to find all books with the word "Python" in the title, or all books published after 2010. You try to do this by checking each book one by one manually.
Manually searching through data is slow and tiring. You might miss some books or make mistakes. Writing many if-else checks for each condition is confusing and hard to change later.
Django's field lookups let you ask the database directly for exactly what you want, like "title contains 'Python'" or "year greater than 2010". This makes your code simple, fast, and easy to read.
books = [b for b in all_books if 'Python' in b.title and b.year > 2010]
books = Book.objects.filter(title__contains='Python', year__gt=2010)
You can quickly and clearly find data matching complex conditions without writing long loops or checks.
A library website shows users all books with "history" in the title published before 2000, updating instantly as new books are added.
Manual data filtering is slow and error-prone.
Field lookups let you ask the database for specific matches easily.
This makes your code cleaner, faster, and easier to maintain.
Practice
Solution
Step 1: Understand the purpose of each lookup
Theexactlookup matches fields that are exactly equal to the given value.containschecks for substring presence,gtmeans greater than, andltmeans less than.Step 2: Match the requirement to the lookup
Since the question asks for exact matches,exactis the correct lookup.Final Answer:
exact -> Option AQuick Check:
Exact match = exact [OK]
- Confusing 'contains' with exact match
- Using 'gt' or 'lt' for equality checks
- Assuming 'exact' is default without specifying
Book for titles containing the word 'django'?Solution
Step 1: Identify the lookup for substring matching
Thecontainslookup is used to find records where the field contains the given substring anywhere inside it.Step 2: Check the syntax for filtering
The correct syntax uses double underscores to specify the lookup:title__contains='django'.Final Answer:
Book.objects.filter(title__contains='django') -> Option CQuick Check:
Substring search = contains [OK]
- Using __exact for substring search
- Using __gt or __lt for string matching
- Missing double underscores in lookup
Product with a field price, what will this query return?Product.objects.filter(price__gt=100)
Solution
Step 1: Understand the lookup used
The lookupprice__gt=100means filter products where the price is greater than 100.Step 2: Interpret the query result
The query returns all products with price values strictly greater than 100, excluding 100 itself.Final Answer:
All products with price greater than 100 -> Option AQuick Check:
gt means greater than [OK]
- Confusing gt with exact or contains
- Thinking gt includes the value 100
- Assuming it filters less than 100
Entry.objects.filter(date__gt='2023-01-01')
What is the most likely cause?
Solution
Step 1: Check field type compatibility
Thegtlookup works with fields that support ordering like DateField or DateTimeField. Ifdateis not one of these, the query will error.Step 2: Validate other options
The lookupgtis valid for date fields, and the string format is acceptable for Django's date parsing. Usingexactis not required.Final Answer:
The field 'date' is not a DateField or DateTimeField -> Option BQuick Check:
gt requires comparable field type [OK]
- Assuming all fields support gt lookup
- Incorrect date string format causing error
- Using exact lookup unnecessarily
Order records where the status field contains 'pending' (case insensitive) and the total is less than 500. Which Django query correctly applies these filters?Solution
Step 1: Choose case-insensitive substring lookup
To find 'pending' regardless of case, useicontainsinstead ofcontains.Step 2: Apply less than filter on total
Thetotal__lt=500filters orders with total less than 500.Step 3: Combine filters correctly
Both filters are passed as keyword arguments tofilter()to apply AND logic.Final Answer:
Order.objects.filter(status__icontains='pending', total__lt=500) -> Option DQuick Check:
Case-insensitive contains + less than = icontains + lt [OK]
- Using contains instead of icontains for case insensitivity
- Mixing lt and gt incorrectly
- Using exact instead of contains for substring search
