Featured
Fix N+1 Queries in Django with select_related and prefetch_related
An N+1 query runs one query for a list and one more for every row. Use select_related for foreign keys, prefetch_related for many-to-many and reverse relations, annotate counts in the database, and pin the query count in a test.