Skip to content

Commit d39e340

Browse files
author
committed
Deployed e221d9a with MkDocs version: 1.6.0
1 parent d1bb692 commit d39e340

25 files changed

Lines changed: 260 additions & 192 deletions

File tree

api-guide/authentication/index.html

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,11 @@ <h1 id="authentication"><a class="toclink" href="#authentication">Authentication
562562
<p>Authentication always runs at the very start of the view, before the permission and throttling checks occur, and before any other code is allowed to proceed.</p>
563563
<p>The <code>request.user</code> property will typically be set to an instance of the <code>contrib.auth</code> package's <code>User</code> class.</p>
564564
<p>The <code>request.auth</code> property is used for any additional authentication information, for example, it may be used to represent an authentication token that the request was signed with.</p>
565-
<hr />
566-
<p><strong>Note:</strong> Don't forget that <strong>authentication by itself won't allow or disallow an incoming request</strong>, it simply identifies the credentials that the request was made with.</p>
565+
<div class="admonition note">
566+
<p class="admonition-title">Note</p>
567+
<p>Don't forget that <strong>authentication by itself won't allow or disallow an incoming request</strong>, it simply identifies the credentials that the request was made with.</p>
567568
<p>For information on how to set up the permission policies for your API please see the <a href="../permissions/">permissions documentation</a>.</p>
568-
<hr />
569+
</div>
569570
<h2 id="how-authentication-is-determined"><a class="toclink" href="#how-authentication-is-determined">How authentication is determined</a></h2>
570571
<p>The authentication schemes are always defined as a list of classes. REST framework will attempt to authenticate with each class in the list, and will set <code>request.user</code> and <code>request.auth</code> using the return value of the first class that successfully authenticates.</p>
571572
<p>If no class authenticates, <code>request.user</code> will be set to an instance of <code>django.contrib.auth.models.AnonymousUser</code>, and <code>request.auth</code> will be set to <code>None</code>.</p>
@@ -638,12 +639,16 @@ <h2 id="basicauthentication"><a class="toclink" href="#basicauthentication">Basi
638639
<p>Unauthenticated responses that are denied permission will result in an <code>HTTP 401 Unauthorized</code> response with an appropriate WWW-Authenticate header. For example:</p>
639640
<pre><code>WWW-Authenticate: Basic realm="api"
640641
</code></pre>
641-
<p><strong>Note:</strong> If you use <code>BasicAuthentication</code> in production you must ensure that your API is only available over <code>https</code>. You should also ensure that your API clients will always re-request the username and password at login, and will never store those details to persistent storage.</p>
642+
<div class="admonition note">
643+
<p class="admonition-title">Note</p>
644+
<p>If you use <code>BasicAuthentication</code> in production you must ensure that your API is only available over <code>https</code>. You should also ensure that your API clients will always re-request the username and password at login, and will never store those details to persistent storage.</p>
645+
</div>
642646
<h2 id="tokenauthentication"><a class="toclink" href="#tokenauthentication">TokenAuthentication</a></h2>
643-
<hr />
644-
<p><strong>Note:</strong> The token authentication provided by Django REST framework is a fairly simple implementation.</p>
647+
<div class="admonition note">
648+
<p class="admonition-title">Note</p>
649+
<p>The token authentication provided by Django REST framework is a fairly simple implementation.</p>
645650
<p>For an implementation which allows more than one token per user, has some tighter security implementation details, and supports token expiry, please see the <a href="https://github.com/James1345/django-rest-knox">Django REST Knox</a> third party package.</p>
646-
<hr />
651+
</div>
647652
<p>This authentication scheme uses a simple token-based HTTP Authentication scheme. Token authentication is appropriate for client-server setups, such as native desktop and mobile clients.</p>
648653
<p>To use the <code>TokenAuthentication</code> scheme you'll need to <a href="#setting-the-authentication-scheme">configure the authentication classes</a> to include <code>TokenAuthentication</code>, and additionally include <code>rest_framework.authtoken</code> in your <code>INSTALLED_APPS</code> setting:</p>
649654
<pre><code>INSTALLED_APPS = [
@@ -674,9 +679,10 @@ <h2 id="tokenauthentication"><a class="toclink" href="#tokenauthentication">Toke
674679
<p>The <code>curl</code> command line tool may be useful for testing token authenticated APIs. For example:</p>
675680
<pre><code>curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
676681
</code></pre>
677-
<hr />
678-
<p><strong>Note:</strong> If you use <code>TokenAuthentication</code> in production you must ensure that your API is only available over <code>https</code>.</p>
679-
<hr />
682+
<div class="admonition note">
683+
<p class="admonition-title">Note</p>
684+
<p>If you use <code>TokenAuthentication</code> in production you must ensure that your API is only available over <code>https</code>.</p>
685+
</div>
680686
<h3 id="generating-tokens"><a class="toclink" href="#generating-tokens">Generating Tokens</a></h3>
681687
<h4 id="by-using-signals"><a class="toclink" href="#by-using-signals">By using signals</a></h4>
682688
<p>If you want every user to have an automatically generated Token, you can simply catch the User's <code>post_save</code> signal.</p>
@@ -795,9 +801,10 @@ <h1 id="custom-authentication"><a class="toclink" href="#custom-authentication">
795801
</ul>
796802
<p>You <em>may</em> also override the <code>.authenticate_header(self, request)</code> method. If implemented, it should return a string that will be used as the value of the <code>WWW-Authenticate</code> header in a <code>HTTP 401 Unauthorized</code> response.</p>
797803
<p>If the <code>.authenticate_header()</code> method is not overridden, the authentication scheme will return <code>HTTP 403 Forbidden</code> responses when an unauthenticated request is denied access.</p>
798-
<hr />
799-
<p><strong>Note:</strong> When your custom authenticator is invoked by the request object's <code>.user</code> or <code>.auth</code> properties, you may see an <code>AttributeError</code> re-raised as a <code>WrappedAttributeError</code>. This is necessary to prevent the original exception from being suppressed by the outer property access. Python will not recognize that the <code>AttributeError</code> originates from your custom authenticator and will instead assume that the request object does not have a <code>.user</code> or <code>.auth</code> property. These errors should be fixed or otherwise handled by your authenticator.</p>
800-
<hr />
804+
<div class="admonition note">
805+
<p class="admonition-title">Note</p>
806+
<p>When your custom authenticator is invoked by the request object's <code>.user</code> or <code>.auth</code> properties, you may see an <code>AttributeError</code> re-raised as a <code>WrappedAttributeError</code>. This is necessary to prevent the original exception from being suppressed by the outer property access. Python will not recognize that the <code>AttributeError</code> originates from your custom authenticator and will instead assume that the request object does not have a <code>.user</code> or <code>.auth</code> property. These errors should be fixed or otherwise handled by your authenticator.</p>
807+
</div>
801808
<h2 id="example"><a class="toclink" href="#example">Example</a></h2>
802809
<p>The following example will authenticate any incoming request as the user given by the username in a custom request header named 'X-USERNAME'.</p>
803810
<pre><code>from django.contrib.auth.models import User

api-guide/caching/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@ <h2 id="using-cache-with-api_view-decorator"><a class="toclink" href="#using-cac
512512
content = {&quot;user_feed&quot;: request.user.get_user_feed()}
513513
return Response(content)
514514
</code></pre>
515-
<p><strong>NOTE:</strong> The <a href="https://docs.djangoproject.com/en/stable/topics/cache/#the-per-view-cache"><code>cache_page</code></a> decorator only caches the
516-
<code>GET</code> and <code>HEAD</code> responses with status 200.</p>
515+
<div class="admonition note">
516+
<p class="admonition-title">Note</p>
517+
<p>The <a href="https://docs.djangoproject.com/en/stable/topics/cache/#the-per-view-cache"><code>cache_page</code></a> decorator only caches the <code>GET</code> and <code>HEAD</code> responses with status 200.</p>
518+
</div>
517519

518520

519521
</div> <!--/span-->

api-guide/content-negotiation/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,11 @@ <h2 id="determining-the-accepted-renderer"><a class="toclink" href="#determining
479479
</ul>
480480
<p>If the requested view was only configured with renderers for <code>YAML</code> and <code>HTML</code>, then REST framework would select whichever renderer was listed first in the <code>renderer_classes</code> list or <code>DEFAULT_RENDERER_CLASSES</code> setting.</p>
481481
<p>For more information on the <code>HTTP Accept</code> header, see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC 2616</a></p>
482-
<hr />
483-
<p><strong>Note</strong>: "q" values are not taken into account by REST framework when determining preference. The use of "q" values negatively impacts caching, and in the author's opinion they are an unnecessary and overcomplicated approach to content negotiation.</p>
482+
<div class="admonition note">
483+
<p class="admonition-title">Note</p>
484+
<p>"q" values are not taken into account by REST framework when determining preference. The use of "q" values negatively impacts caching, and in the author's opinion they are an unnecessary and overcomplicated approach to content negotiation.</p>
484485
<p>This is a valid approach as the HTTP spec deliberately underspecifies how a server should weight server-based preferences against client-based preferences.</p>
485-
<hr />
486+
</div>
486487
<h1 id="custom-content-negotiation"><a class="toclink" href="#custom-content-negotiation">Custom content negotiation</a></h1>
487488
<p>It's unlikely that you'll want to provide a custom content negotiation scheme for REST framework, but you can do so if needed. To implement a custom content negotiation scheme override <code>BaseContentNegotiation</code>.</p>
488489
<p>REST framework's content negotiation classes handle selection of both the appropriate parser for the request, and the appropriate renderer for the response, so you should implement both the <code>.select_parser(request, parsers)</code> and <code>.select_renderer(request, renderers, format_suffix)</code> methods.</p>

api-guide/fields/index.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,10 @@ <h1 id="serializer-fields"><a class="toclink" href="#serializer-fields">Serializ
652652
<p>&mdash; <a href="https://docs.djangoproject.com/en/stable/ref/forms/api/#django.forms.Form.cleaned_data">Django documentation</a></p>
653653
</blockquote>
654654
<p>Serializer fields handle converting between primitive values and internal datatypes. They also deal with validating input values, as well as retrieving and setting the values from their parent objects.</p>
655-
<hr />
656-
<p><strong>Note:</strong> The serializer fields are declared in <code>fields.py</code>, but by convention you should import them using <code>from rest_framework import serializers</code> and refer to fields as <code>serializers.&lt;FieldName&gt;</code>.</p>
657-
<hr />
655+
<div class="admonition note">
656+
<p class="admonition-title">Note</p>
657+
<p>The serializer fields are declared in <code>fields.py</code>, but by convention you should import them using <code>from rest_framework import serializers</code> and refer to fields as <code>serializers.&lt;FieldName&gt;</code>.</p>
658+
</div>
658659
<h2 id="core-arguments"><a class="toclink" href="#core-arguments">Core arguments</a></h2>
659660
<p>Each serializer field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:</p>
660661
<h3 id="read_only"><a class="toclink" href="#read_only"><code>read_only</code></a></h3>
@@ -1030,9 +1031,10 @@ <h2 id="hiddenfield"><a class="toclink" href="#hiddenfield">HiddenField</a></h2>
10301031
</code></pre>
10311032
<p>The <code>HiddenField</code> class is usually only needed if you have some validation that needs to run based on some pre-provided field values, but you do not want to expose all of those fields to the end user.</p>
10321033
<p>For further examples on <code>HiddenField</code> see the <a href="../validators/">validators</a> documentation.</p>
1033-
<hr />
1034-
<p><strong>Note:</strong> <code>HiddenField()</code> does not appear in <code>partial=True</code> serializer (when making <code>PATCH</code> request).</p>
1035-
<hr />
1034+
<div class="admonition note">
1035+
<p class="admonition-title">Note</p>
1036+
<p><code>HiddenField()</code> does not appear in <code>partial=True</code> serializer (when making <code>PATCH</code> request).</p>
1037+
</div>
10361038
<h2 id="modelfield"><a class="toclink" href="#modelfield">ModelField</a></h2>
10371039
<p>A generic field that can be tied to any arbitrary model field. The <code>ModelField</code> class delegates the task of serialization/deserialization to its associated model field. This field can be used to create serializer fields for custom model fields, without having to create a new custom serializer field.</p>
10381040
<p>This field is used by <code>ModelSerializer</code> to correspond to custom model field classes.</p>

api-guide/generic-views/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,10 @@ <h4 id="get_querysetself"><a class="toclink" href="#get_querysetself"><code>get_
623623
user = self.request.user
624624
return user.accounts.all()
625625
</code></pre>
626-
<hr />
627-
<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>
628-
<hr />
626+
<div class="admonition tip">
627+
<p class="admonition-title">Tip</p>
628+
<p>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>
629+
</div>
629630
<h3 id="avoiding-n1-queries"><a class="toclink" href="#avoiding-n1-queries">Avoiding N+1 Queries</a></h3>
630631
<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>
631632
<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>

api-guide/parsers/index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,12 @@ <h1 id="parsers"><a class="toclink" href="#parsers">Parsers</a></h1>
522522
<p>REST framework includes a number of built-in Parser classes, that allow you to accept requests with various media types. There is also support for defining your own custom parsers, which gives you the flexibility to design the media types that your API accepts.</p>
523523
<h2 id="how-the-parser-is-determined"><a class="toclink" href="#how-the-parser-is-determined">How the parser is determined</a></h2>
524524
<p>The set of valid parsers for a view is always defined as a list of classes. When <code>request.data</code> is accessed, REST framework will examine the <code>Content-Type</code> header on the incoming request, and determine which parser to use to parse the request content.</p>
525-
<hr />
526-
<p><strong>Note</strong>: When developing client applications always remember to make sure you're setting the <code>Content-Type</code> header when sending data in an HTTP request.</p>
527-
<p>If you don't set the content type, most clients will default to using <code>'application/x-www-form-urlencoded'</code>, which may not be what you wanted.</p>
525+
<div class="admonition note">
526+
<p class="admonition-title">Note</p>
527+
<p>When developing client applications always remember to make sure you're setting the <code>Content-Type</code> header when sending data in an HTTP request.</p>
528+
<p>If you don't set the content type, most clients will default to using <code>'application/x-www-form-urlencoded'</code>, which may not be what you want.</p>
528529
<p>As an example, if you are sending <code>json</code> encoded data using jQuery with the <a href="https://api.jquery.com/jQuery.ajax/">.ajax() method</a>, you should make sure to include the <code>contentType: 'application/json'</code> setting.</p>
529-
<hr />
530+
</div>
530531
<h2 id="setting-the-parsers"><a class="toclink" href="#setting-the-parsers">Setting the parsers</a></h2>
531532
<p>The default set of parsers may be set globally, using the <code>DEFAULT_PARSER_CLASSES</code> setting. For example, the following settings would allow only requests with <code>JSON</code> content, instead of the default of JSON or form data.</p>
532533
<pre><code>REST_FRAMEWORK = {

0 commit comments

Comments
 (0)