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.
Jump into concepts and practice - no test required
Book.objects.filter(title__exact='Django') Book.objects.filter(title__contains='Django') Book.objects.filter(pages__gt=100) Book.objects.filter(pages__lt=50)
| Step | Query | Lookup Type | Condition Evaluated | Rows Matched | Result |
|---|---|---|---|---|---|
| 1 | title__exact='Django' | exact | title == 'Django' | 2 | Returns 2 books with title exactly 'Django' |
| 2 | title__contains='Django' | contains | 'Django' in title | 3 | Returns 3 books with 'Django' in title |
| 3 | pages__gt=100 | gt | pages > 100 | 4 | Returns 4 books with pages greater than 100 |
| 4 | pages__lt=50 | lt | pages < 50 | 1 | Returns 1 book with pages less than 50 |
| 5 | End | - | - | - | No more queries |
| Variable | Start | After Query 1 | After Query 2 | After Query 3 | After Query 4 | Final |
|---|---|---|---|---|---|---|
| filtered_books | [] | [Book1, Book2] | [Book1, Book2, Book3] | [Book4, Book5, Book6, Book7] | [Book8] | Final filtered sets per query |
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).
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.exact is the correct lookup.Book for titles containing the word 'django'?contains lookup is used to find records where the field contains the given substring anywhere inside it.title__contains='django'.Product with a field price, what will this query return?Product.objects.filter(price__gt=100)
price__gt=100 means filter products where the price is greater than 100.Entry.objects.filter(date__gt='2023-01-01')
gt lookup works with fields that support ordering like DateField or DateTimeField. If date is not one of these, the query will error.gt is valid for date fields, and the string format is acceptable for Django's date parsing. Using exact is not required.Order records where the status field contains 'pending' (case insensitive) and the total is less than 500. Which Django query correctly applies these filters?icontains instead of contains.total__lt=500 filters orders with total less than 500.filter() to apply AND logic.