Performance: Filtering with django-filter
MEDIUM IMPACT
This affects server response time and the amount of data sent to the client, impacting page load speed and interaction responsiveness.
import django_filters class ProductFilter(django_filters.FilterSet): class Meta: model = Product fields = ['category'] def product_list(request): f = ProductFilter(request.GET, queryset=Product.objects.all()) return render(request, 'products.html', {'filter': f, 'products': f.qs})
def product_list(request): products = Product.objects.all() if 'category' in request.GET: products = [p for p in products if p.category == request.GET['category']] return render(request, 'products.html', {'products': products})
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Python list filtering after query | N/A (server-side) | N/A | Large HTML response slows paint | [X] Bad |
| django-filter database filtering | N/A (server-side) | N/A | Smaller HTML response speeds paint | [OK] Good |