Performance: Search and filter options
This affects page load speed and interaction responsiveness by controlling how much data is processed and sent to the browser.
filtered = Model.objects.filter(name__icontains=search_term) # Filtering done in database query
results = Model.objects.all() filtered = [item for item in results if search_term in item.name] # Filtering done in Python after fetching all data
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Filtering in Python after fetching all data | High (many DOM nodes) | Many reflows due to large DOM | High paint cost | [X] Bad |
| Filtering in database query | Low (few DOM nodes) | Single reflow for filtered results | Low paint cost | [OK] Good |
filter() do when used with icontains in a query?filter() usagefilter() method in Django ORM is used to select records that match certain conditions.icontains lookupicontains checks if a field contains a substring, ignoring case differences.Book by title containing the word 'django' ignoring case?title__icontains is correct.filter() returns a queryset; get() returns a single object and may error if multiple matches.books contain if the URL query string is ?search=python?
def book_list(request):
query = request.GET.get('search', '')
books = Book.objects.filter(title__icontains=query)
return render(request, 'books.html', {'books': books})title__icontains='python', so it returns books with titles containing 'python' ignoring case.Product objects by a search term from the URL:
def product_search(request):
term = request.GET['search']
results = Product.objects.filter(name_icontains=term)
return render(request, 'search.html', {'results': results})name_icontains, but Django requires double underscores between field and lookup: name__icontains.request.GET['search'] is valid but may raise KeyError if missing; render() usage is correct with request parameter.name__icontains. -> Option BArticle objects by a search term in the title and also filters by a minimum number of views provided by the user. Which of the following code snippets correctly applies both filters using values from request.GET?get() with default values avoids errors. Converting min_views to int is necessary for numeric comparison.filter() with multiple conditions joined by commas applies AND logic. title__icontains=query and views__gte=min_views correctly filter by title substring and minimum views.Pass the test and this pattern moves unknown