mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-10-22 19:54:50 +03:00
Deployed 653343c
with MkDocs version: 1.6.0
This commit is contained in:
parent
c6b5d6a7de
commit
8de1ecf19b
|
@ -630,6 +630,29 @@ class UserList(generics.ListCreateAPIView):
|
||||||
<hr />
|
<hr />
|
||||||
<p><strong>Note:</strong> If the <code>serializer_class</code> used in the generic view spans orm relations, leading to an n+1 problem, you could optimize your queryset in this method using <code>select_related</code> and <code>prefetch_related</code>. To get more information about n+1 problem and use cases of the mentioned methods refer to related section in <a href="https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.select_related">django documentation</a>.</p>
|
<p><strong>Note:</strong> If the <code>serializer_class</code> used in the generic view spans orm relations, leading to an n+1 problem, you could optimize your queryset in this method using <code>select_related</code> and <code>prefetch_related</code>. To get more information about n+1 problem and use cases of the mentioned methods refer to related section in <a href="https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.select_related">django documentation</a>.</p>
|
||||||
<hr />
|
<hr />
|
||||||
|
<h3 id="avoiding-n1-queries"><a class="toclink" href="#avoiding-n1-queries">Avoiding N+1 Queries</a></h3>
|
||||||
|
<p>When listing objects (e.g. using <code>ListAPIView</code> or <code>ModelViewSet</code>), serializers may trigger an N+1 query pattern if related objects are accessed individually for each item.</p>
|
||||||
|
<p>To prevent this, optimize the queryset in <code>get_queryset()</code> or by setting the <code>queryset</code> class attribute using <a href="https://docs.djangoproject.com/en/stable/ref/models/querysets/#select-related"><code>select_related()</code></a> and <a href="https://docs.djangoproject.com/en/stable/ref/models/querysets/#prefetch-related"><code>prefetch_related()</code></a>, depending on the type of relationship.</p>
|
||||||
|
<p><strong>For ForeignKey and OneToOneField</strong>:</p>
|
||||||
|
<p>Use <code>select_related()</code> to fetch related objects in the same query:</p>
|
||||||
|
<pre><code>def get_queryset(self):
|
||||||
|
return Order.objects.select_related("customer", "billing_address")
|
||||||
|
</code></pre>
|
||||||
|
<p><strong>For reverse and many-to-many relationships</strong>:</p>
|
||||||
|
<p>Use <code>prefetch_related()</code> to efficiently load collections of related objects:</p>
|
||||||
|
<pre><code>def get_queryset(self):
|
||||||
|
return Book.objects.prefetch_related("categories", "reviews__user")
|
||||||
|
</code></pre>
|
||||||
|
<p><strong>Combining both</strong>:</p>
|
||||||
|
<pre><code>def get_queryset(self):
|
||||||
|
return (
|
||||||
|
Order.objects
|
||||||
|
.select_related("customer")
|
||||||
|
.prefetch_related("items__product")
|
||||||
|
)
|
||||||
|
</code></pre>
|
||||||
|
<p>These optimizations reduce repeated database access and improve list view performance.</p>
|
||||||
|
<hr />
|
||||||
<h4 id="get_objectself"><a class="toclink" href="#get_objectself"><code>get_object(self)</code></a></h4>
|
<h4 id="get_objectself"><a class="toclink" href="#get_objectself"><code>get_object(self)</code></a></h4>
|
||||||
<p>Returns an object instance that should be used for detail views. Defaults to using the <code>lookup_field</code> parameter to filter the base queryset.</p>
|
<p>Returns an object instance that should be used for detail views. Defaults to using the <code>lookup_field</code> parameter to filter the base queryset.</p>
|
||||||
<p>May be overridden to provide more complex behavior, such as object lookups based on more than one URL kwarg.</p>
|
<p>May be overridden to provide more complex behavior, such as object lookups based on more than one URL kwarg.</p>
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user