You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/generic-views.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,6 +102,39 @@ For example:
102
102
103
103
---
104
104
105
+
### Avoiding N+1 Queries
106
+
107
+
When listing objects (e.g. using `ListAPIView` or `ModelViewSet`), serializers may trigger an N+1 query pattern if related objects are accessed individually for each item.
108
+
109
+
To prevent this, optimize the queryset in `get_queryset()` using `select_related()` and `prefetch_related()` depending on the type of relationship.
110
+
111
+
**For ForeignKey and OneToOneField**:
112
+
113
+
Use `select_related()` to fetch related objects in the same query:
0 commit comments