Performance: Field lookups (exact, contains, gt, lt)
This affects database query speed and server response time, impacting how fast data is fetched and displayed.
Jump into concepts and practice - no test required
MyModel.objects.filter(name__exact='abc')
MyModel.objects.filter(name__contains='abc')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using 'contains' on unindexed text field | N/A | N/A | N/A | [X] Bad |
| Using 'exact' on indexed field | N/A | N/A | N/A | [OK] Good |
| Using 'gt' and 'lt' on unindexed numeric field | N/A | N/A | N/A | [X] Bad |
| Using 'range' on indexed numeric field | N/A | N/A | N/A | [OK] Good |
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.