Performance: Prefetch_related for reverse relations
HIGH IMPACT
This affects database query efficiency and page load speed by reducing the number of queries needed to fetch related data.
authors = Author.objects.prefetch_related('book_set').all() for author in authors: print(author.book_set.all())
authors = Author.objects.all() for author in authors: print(author.book_set.all())
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without prefetch_related (N+1 queries) | Minimal DOM impact | Minimal | Minimal | [X] Bad - slow data fetching delays rendering |
| With prefetch_related (2 queries) | Minimal DOM impact | Minimal | Minimal | [OK] Good - fast data fetching improves LCP |