Skip to content

Commit 469ca95

Browse files
committed
Deployed c05998f with MkDocs version: 1.1.2
1 parent dee977c commit 469ca95

File tree

27 files changed

+160
-87
lines changed

27 files changed

+160
-87
lines changed

api-guide/fields/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,12 @@ <h3 id="allow_null"><a class="toclink" href="#allow_null"><code>allow_null</code
683683
<p>Note that, without an explicit <code>default</code>, setting this argument to <code>True</code> will imply a <code>default</code> value of <code>null</code> for serialization output, but does not imply a default for input deserialization.</p>
684684
<p>Defaults to <code>False</code></p>
685685
<h3 id="source"><a class="toclink" href="#source"><code>source</code></a></h3>
686-
<p>The name of the attribute that will be used to populate the field. May be a method that only takes a <code>self</code> argument, such as <code>URLField(source='get_absolute_url')</code>, or may use dotted notation to traverse attributes, such as <code>EmailField(source='user.email')</code>. When serializing fields with dotted notation, it may be necessary to provide a <code>default</code> value if any object is not present or is empty during attribute traversal.</p>
686+
<p>The name of the attribute that will be used to populate the field. May be a method that only takes a <code>self</code> argument, such as <code>URLField(source='get_absolute_url')</code>, or may use dotted notation to traverse attributes, such as <code>EmailField(source='user.email')</code>. </p>
687+
<p>When serializing fields with dotted notation, it may be necessary to provide a <code>default</code> value if any object is not present or is empty during attribute traversal. Beware of possible n+1 problems when using source attribute if you are accessing a relational orm model. For example:</p>
688+
<pre><code>class CommentSerializer(serializers.Serializer):
689+
email = serializers.EmailField(source="user.email")
690+
</code></pre>
691+
<p>would require user object to be fetched from database when it is not prefetched. If that is not wanted, be sure to be using <code>prefetch_related</code> and <code>select_related</code> methods appropriately. For more information about the methods refer to <a href="https://docs.djangoproject.com/en/3.1/ref/models/querysets/#django.db.models.query.QuerySet.select_related">django documentation</a>.</p>
687692
<p>The value <code>source='*'</code> has a special meaning, and is used to indicate that the entire object should be passed through to the field. This can be useful for creating nested representations, or for fields which require access to the complete object in order to determine the output representation.</p>
688693
<p>Defaults to the name of the field.</p>
689694
<h3 id="validators"><a class="toclink" href="#validators"><code>validators</code></a></h3>
@@ -850,7 +855,7 @@ <h2 id="datetimefield"><a class="toclink" href="#datetimefield">DateTimeField</a
850855
<ul>
851856
<li><code>format</code> - A string representing the output format. If not specified, this defaults to the same value as the <code>DATETIME_FORMAT</code> settings key, which will be <code>'iso-8601'</code> unless set. Setting to a format string indicates that <code>to_representation</code> return values should be coerced to string output. Format strings are described below. Setting this value to <code>None</code> indicates that Python <code>datetime</code> objects should be returned by <code>to_representation</code>. In this case the datetime encoding will be determined by the renderer.</li>
852857
<li><code>input_formats</code> - A list of strings representing the input formats which may be used to parse the date. If not specified, the <code>DATETIME_INPUT_FORMATS</code> setting will be used, which defaults to <code>['iso-8601']</code>.</li>
853-
<li><code>default_timezone</code> - A <code>pytz.timezone</code> representing the timezone. If not specified and the <code>USE_TZ</code> setting is enabled, this defaults to the <a href="https://docs.djangoproject.com/en/stable/topics/i18n/timezones/#default-time-zone-and-current-time-zone">current timezone</a>. If <code>USE_TZ</code> is disabled, then datetime objects will be naive.</li>
858+
<li><code>default_timezone</code> - A <code>tzinfo</code> subclass (<code>zoneinfo</code> or <code>pytz</code>) prepresenting the timezone. If not specified and the <code>USE_TZ</code> setting is enabled, this defaults to the <a href="https://docs.djangoproject.com/en/stable/topics/i18n/timezones/#default-time-zone-and-current-time-zone">current timezone</a>. If <code>USE_TZ</code> is disabled, then datetime objects will be naive.</li>
854859
</ul>
855860
<h4 id="datetimefield-format-strings"><a class="toclink" href="#datetimefield-format-strings"><code>DateTimeField</code> format strings.</a></h4>
856861
<p>Format strings may either be <a href="https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior">Python strftime formats</a> which explicitly specify the format, or the special string <code>'iso-8601'</code>, which indicates that <a href="https://www.w3.org/TR/NOTE-datetime">ISO 8601</a> style datetimes should be used. (eg <code>'2013-01-29T12:34:56.000000Z'</code>)</p>

api-guide/filtering/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ <h2 id="searchfilter"><a class="toclink" href="#searchfilter">SearchFilter</a></
703703
def get_search_fields(self, view, request):
704704
if request.query_params.get('title_only'):
705705
return ['title']
706-
return super(CustomSearchFilter, self).get_search_fields(view, request)
706+
return super().get_search_fields(view, request)
707707
</code></pre>
708708
<p>For more details, see the <a href="https://docs.djangoproject.com/en/stable/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields">Django documentation</a>.</p>
709709
<hr />

api-guide/generic-views/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,9 @@ <h4 id="get_querysetself"><a class="toclink" href="#get_querysetself"><code>get_
615615
user = self.request.user
616616
return user.accounts.all()
617617
</code></pre>
618+
<hr />
619+
<p><strong>Note:</strong> If the serializer_class 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/3.1/ref/models/querysets/#django.db.models.query.QuerySet.select_related">django documentation</a>.</p>
620+
<hr />
618621
<h4 id="get_objectself"><a class="toclink" href="#get_objectself"><code>get_object(self)</code></a></h4>
619622
<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>
620623
<p>May be overridden to provide more complex behavior, such as object lookups based on more than one URL kwarg.</p>

api-guide/pagination/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ <h2 id="drf-extensions"><a class="toclink" href="#drf-extensions">DRF-extensions
729729
<h2 id="drf-proxy-pagination"><a class="toclink" href="#drf-proxy-pagination">drf-proxy-pagination</a></h2>
730730
<p>The <a href="https://github.com/tuffnatty/drf-proxy-pagination"><code>drf-proxy-pagination</code> package</a> includes a <code>ProxyPagination</code> class which allows to choose pagination class with a query parameter.</p>
731731
<h2 id="link-header-pagination"><a class="toclink" href="#link-header-pagination">link-header-pagination</a></h2>
732-
<p>The <a href="https://github.com/tbeadle/django-rest-framework-link-header-pagination"><code>django-rest-framework-link-header-pagination</code> package</a> includes a <code>LinkHeaderPagination</code> class which provides pagination via an HTTP <code>Link</code> header as described in <a href="github-link-pagination">Github's developer documentation</a>.</p>
732+
<p>The <a href="https://github.com/tbeadle/django-rest-framework-link-header-pagination"><code>django-rest-framework-link-header-pagination</code> package</a> includes a <code>LinkHeaderPagination</code> class which provides pagination via an HTTP <code>Link</code> header as described in <a href="https://docs.github.com/en/rest/guides/traversing-with-pagination">GitHub REST API documentation</a>.</p>
733733

734734

735735
</div> <!--/span-->

api-guide/permissions/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ <h1 id="permissions"><a class="toclink" href="#permissions">Permissions</a></h1>
543543
<h2 id="how-permissions-are-determined"><a class="toclink" href="#how-permissions-are-determined">How permissions are determined</a></h2>
544544
<p>Permissions in REST framework are always defined as a list of permission classes.</p>
545545
<p>Before running the main body of the view each permission in the list is checked.
546-
If any permission check fails an <code>exceptions.PermissionDenied</code> or <code>exceptions.NotAuthenticated</code> exception will be raised, and the main body of the view will not run.</p>
547-
<p>When the permissions checks fail either a "403 Forbidden" or a "401 Unauthorized" response will be returned, according to the following rules:</p>
546+
If any permission check fails, an <code>exceptions.PermissionDenied</code> or <code>exceptions.NotAuthenticated</code> exception will be raised, and the main body of the view will not run.</p>
547+
<p>When the permission checks fail, either a "403 Forbidden" or a "401 Unauthorized" response will be returned, according to the following rules:</p>
548548
<ul>
549549
<li>The request was successfully authenticated, but permission was denied. <em>&mdash; An HTTP 403 Forbidden response will be returned.</em></li>
550550
<li>The request was not successfully authenticated, and the highest priority authentication class <em>does not</em> use <code>WWW-Authenticate</code> headers. <em>&mdash; An HTTP 403 Forbidden response will be returned.</em></li>
@@ -753,7 +753,7 @@ <h1 id="overview-of-access-restriction-methods"><a class="toclink" href="#overvi
753753
<tr>
754754
<td>Action: list</td>
755755
<td>global</td>
756-
<td>no</td>
756+
<td>global</td>
757757
<td>object-level*</td>
758758
</tr>
759759
<tr>

api-guide/relations/index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,31 @@ <h1 id="serializer-relations"><a class="toclink" href="#serializer-relations">Se
547547
<hr />
548548
<p><strong>Note:</strong> The relational fields are declared in <code>relations.py</code>, but by convention you should import them from the <code>serializers</code> module, using <code>from rest_framework import serializers</code> and refer to fields as <code>serializers.&lt;FieldName&gt;</code>.</p>
549549
<hr />
550+
<hr />
551+
<p><strong>Note:</strong> REST Framework does not attempt to automatically optimize querysets passed to serializers in terms of <code>select_related</code> and <code>prefetch_related</code> since it would be too much magic. A serializer with a field spanning an orm relation through its source attribute could require an additional database hit to fetch related object from the database. It is the programmer's responsibility to optimize queries to avoid additional database hits which could occur while using such a serializer.</p>
552+
<p>For example, the following serializer would lead to a database hit each time evaluating the tracks field if it is not prefetched:</p>
553+
<pre><code>class AlbumSerializer(serializers.ModelSerializer):
554+
tracks = serializers.SlugRelatedField(
555+
many=True,
556+
read_only=True,
557+
slug_field='title'
558+
)
559+
560+
class Meta:
561+
model = Album
562+
fields = ['album_name', 'artist', 'tracks']
563+
564+
# For each album object, tracks should be fetched from database
565+
qs = Album.objects.all()
566+
print(AlbumSerializer(qs, many=True).data)
567+
</code></pre>
568+
<p>If <code>AlbumSerializer</code> is used to serialize a fairly large queryset with <code>many=True</code> then it could be a serious performance problem. Optimizing the queryset passed to <code>AlbumSerializer</code> with:</p>
569+
<pre><code>qs = Album.objects.prefetch_related('tracks')
570+
# No additional database hits required
571+
print(AlbumSerializer(qs, many=True).data)
572+
</code></pre>
573+
<p>would solve the issue.</p>
574+
<hr />
550575
<h4 id="inspecting-relationships"><a class="toclink" href="#inspecting-relationships">Inspecting relationships.</a></h4>
551576
<p>When using the <code>ModelSerializer</code> class, serializer fields and relationships will be automatically generated for you. Inspecting these automatically generated fields can be a useful tool for determining how to customize the relationship style.</p>
552577
<p>To do so, open the Django shell, using <code>python manage.py shell</code>, then import the serializer class, instantiate it, and print the object representation…</p>

api-guide/serializers/index.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@ <h3 id="myModalLabel">Documentation search</h3>
519519
<a href="#allow_empty">allow_empty</a>
520520
</li>
521521

522+
<li>
523+
<a href="#max_length">max_length</a>
524+
</li>
525+
526+
<li>
527+
<a href="#min_length">min_length</a>
528+
</li>
529+
522530
<li>
523531
<a href="#customizing-listserializer-behavior">Customizing ListSerializer behavior</a>
524532
</li>
@@ -741,7 +749,7 @@ <h2 id="saving-instances"><a class="toclink" href="#saving-instances">Saving ins
741749
# .save() will update the existing `comment` instance.
742750
serializer = CommentSerializer(comment, data=data)
743751
</code></pre>
744-
<p>Both the <code>.create()</code> and <code>.update()</code> methods are optional. You can implement either neither, one, or both of them, depending on the use-case for your serializer class.</p>
752+
<p>Both the <code>.create()</code> and <code>.update()</code> methods are optional. You can implement either none, one, or both of them, depending on the use-case for your serializer class.</p>
745753
<h4 id="passing-additional-attributes-to-save"><a class="toclink" href="#passing-additional-attributes-to-save">Passing additional attributes to <code>.save()</code></a></h4>
746754
<p>Sometimes you'll want your view code to be able to inject additional data at the point of saving the instance. This additional data might include information like the current user, the current time, or anything else that is not part of the request data.</p>
747755
<p>You can do so by including additional keyword arguments when calling <code>.save()</code>. For example:</p>
@@ -1204,6 +1212,10 @@ <h1 id="listserializer"><a class="toclink" href="#listserializer">ListSerializer
12041212
<p>The following argument can also be passed to a <code>ListSerializer</code> field or a serializer that is passed <code>many=True</code>:</p>
12051213
<h3 id="allow_empty"><a class="toclink" href="#allow_empty"><code>allow_empty</code></a></h3>
12061214
<p>This is <code>True</code> by default, but can be set to <code>False</code> if you want to disallow empty lists as valid input.</p>
1215+
<h3 id="max_length"><a class="toclink" href="#max_length"><code>max_length</code></a></h3>
1216+
<p>This is <code>None</code> by default, but can be set to a positive integer if you want to validates that the list contains no more than this number of elements.</p>
1217+
<h3 id="min_length"><a class="toclink" href="#min_length"><code>min_length</code></a></h3>
1218+
<p>This is <code>None</code> by default, but can be set to a positive integer if you want to validates that the list contains no fewer than this number of elements.</p>
12071219
<h3 id="customizing-listserializer-behavior"><a class="toclink" href="#customizing-listserializer-behavior">Customizing <code>ListSerializer</code> behavior</a></h3>
12081220
<p>There <em>are</em> a few use cases when you might want to customize the <code>ListSerializer</code> behavior. For example:</p>
12091221
<ul>
@@ -1483,7 +1495,7 @@ <h3 id="example"><a class="toclink" href="#example">Example</a></h3>
14831495
fields = kwargs.pop('fields', None)
14841496

14851497
# Instantiate the superclass normally
1486-
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
1498+
super().__init__(*args, **kwargs)
14871499

14881500
if fields is not None:
14891501
# Drop any fields that are not specified in the `fields` argument.

api-guide/testing/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ <h2 id="csrf"><a class="toclink" href="#csrf">CSRF</a></h2>
713713
<p>If you're using <code>SessionAuthentication</code> then you'll need to include a CSRF token
714714
for any <code>POST</code>, <code>PUT</code>, <code>PATCH</code> or <code>DELETE</code> requests.</p>
715715
<p>You can do so by following the same flow that a JavaScript based client would use.
716-
First make a <code>GET</code> request in order to obtain a CRSF token, then present that
716+
First, make a <code>GET</code> request in order to obtain a CSRF token, then present that
717717
token in the following request.</p>
718718
<p>For example...</p>
719719
<pre><code>client = RequestsClient()
@@ -734,7 +734,7 @@ <h2 id="live-tests"><a class="toclink" href="#live-tests">Live tests</a></h2>
734734
<p>With careful usage both the <code>RequestsClient</code> and the <code>CoreAPIClient</code> provide
735735
the ability to write test cases that can run either in development, or be run
736736
directly against your staging server or production environment.</p>
737-
<p>Using this style to create basic tests of a few core piece of functionality is
737+
<p>Using this style to create basic tests of a few core pieces of functionality is
738738
a powerful way to validate your live service. Doing so may require some careful
739739
attention to setup and teardown to ensure that the tests run in a way that they
740740
do not directly affect customer data.</p>

api-guide/views/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ <h2 id="api_view"><a class="toclink" href="#api_view">@api_view()</a></h2>
558558
<p><strong>Signature:</strong> <code>@api_view(http_method_names=['GET'])</code></p>
559559
<p>The core of this functionality is the <code>api_view</code> decorator, which takes a list of HTTP methods that your view should respond to. For example, this is how you would write a very simple view that just manually returns some data:</p>
560560
<pre><code>from rest_framework.decorators import api_view
561+
from rest_framework.response import Response
561562

562563
@api_view()
563564
def hello_world(request):

api-guide/viewsets/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ <h2 id="introspecting-viewset-actions"><a class="toclink" href="#introspecting-v
587587
if self.action == 'list':
588588
permission_classes = [IsAuthenticated]
589589
else:
590-
permission_classes = [IsAdmin]
590+
permission_classes = [IsAdminUser]
591591
return [permission() for permission in permission_classes]
592592
</code></pre>
593593
<h2 id="marking-extra-actions-for-routing"><a class="toclink" href="#marking-extra-actions-for-routing">Marking extra actions for routing</a></h2>

0 commit comments

Comments
 (0)