Field lookups help you find specific data in your database by matching conditions easily.
Field lookups (exact, contains, gt, lt) in Django
Start learning this pattern below
Jump into concepts and practice - no test required
Model.objects.filter(field__lookup=value)Use double underscores __ to separate the field name and the lookup type.
Common lookups include exact, contains, gt (greater than), and lt (less than).
User.objects.filter(name__exact='Alice')
Product.objects.filter(description__contains='organic')
Order.objects.filter(amount__gt=100)
Event.objects.filter(date__lt='2024-01-01')
This example defines a Product model and shows how to use field lookups to filter products by exact name, description containing a word, price greater than, and price less than.
Printing the query shows the SQL Django will run.
from django.db import models class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField() price = models.DecimalField(max_digits=6, decimal_places=2) # Example queries products_exact = Product.objects.filter(name__exact='Apple') products_contains = Product.objects.filter(description__contains='fresh') products_gt = Product.objects.filter(price__gt=10.00) products_lt = Product.objects.filter(price__lt=5.00) # For demonstration, print the querysets' query strings print(products_exact.query) print(products_contains.query) print(products_gt.query) print(products_lt.query)
Field lookups are case-sensitive by default. Use icontains for case-insensitive contains.
Lookups can be combined with other filters for more complex queries.
Field lookups let you filter database records by conditions on fields.
Use exact for exact matches, contains for substring matches, gt and lt for greater or less than comparisons.
They make querying your data simple and readable.
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
