mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-13 13:16:55 +03:00
Update documentation
This commit is contained in:
parent
d8dbd86790
commit
987880e131
|
@ -648,7 +648,7 @@ python manage.py createsuperuser
|
|||
<li><code>request.auth</code> will be <code>None</code>.</li>
|
||||
</ul>
|
||||
<p>Unauthenticated responses that are denied permission will result in an <code>HTTP 403 Forbidden</code> response.</p>
|
||||
<p>If you're using an AJAX style API with SessionAuthentication, you'll need to make sure you include a valid CSRF token for any "unsafe" HTTP method calls, such as <code>PUT</code>, <code>PATCH</code>, <code>POST</code> or <code>DELETE</code> requests. See the <a href="https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax">Django CSRF documentation</a> for more details.</p>
|
||||
<p>If you're using an AJAX style API with SessionAuthentication, you'll need to make sure you include a valid CSRF token for any "unsafe" HTTP method calls, such as <code>PUT</code>, <code>PATCH</code>, <code>POST</code> or <code>DELETE</code> requests. See the <a href="https://docs.djangoproject.com/en/dev/ref/csrf/#ajax">Django CSRF documentation</a> for more details.</p>
|
||||
<h2 id="oauthauthentication">OAuthAuthentication</h2>
|
||||
<p>This authentication uses <a href="http://oauth.net/core/1.0a">OAuth 1.0a</a> authentication scheme. OAuth 1.0a provides signature validation which provides a reasonable level of security over plain non-HTTPS connections. However, it may also be considered more complicated than OAuth2, as it requires clients to sign their requests.</p>
|
||||
<p>This authentication class depends on the optional <code>django-oauth-plus</code> and <code>oauth2</code> packages. In order to make it work you must install these packages and add <code>oauth_provider</code> to your <code>INSTALLED_APPS</code>:</p>
|
||||
|
|
|
@ -405,6 +405,10 @@
|
|||
<a href="#urlfield">URLField</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="#uuidfield">UUIDField</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -493,6 +497,10 @@
|
|||
<a href="#listfield">ListField</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="#dictfield">DictField</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -674,6 +682,10 @@ color_channel = serializers.ChoiceField(
|
|||
<p>A <code>RegexField</code> that validates the input against a URL matching pattern. Expects fully qualified URLs of the form <code>http://<host>/<path></code>.</p>
|
||||
<p>Corresponds to <code>django.db.models.fields.URLField</code>. Uses Django's <code>django.core.validators.URLValidator</code> for validation.</p>
|
||||
<p><strong>Signature:</strong> <code>URLField(max_length=200, min_length=None, allow_blank=False)</code></p>
|
||||
<h2 id="uuidfield">UUIDField</h2>
|
||||
<p>A field that ensures the input is a valid UUID string. The <code>to_internal_value</code> method will return a <code>uuid.UUID</code> instance. On output the field will return a string in the canonical hyphenated format, for example:</p>
|
||||
<pre><code>"de305d54-75b4-431b-adb2-eb6b9e546013"
|
||||
</code></pre>
|
||||
<hr />
|
||||
<h1 id="numeric-fields">Numeric fields</h1>
|
||||
<h2 id="integerfield">IntegerField</h2>
|
||||
|
@ -767,7 +779,7 @@ color_channel = serializers.ChoiceField(
|
|||
</ul>
|
||||
<p>Both the <code>allow_blank</code> and <code>allow_null</code> are valid options on <code>ChoiceField</code>, although it is highly recommended that you only use one and not both. <code>allow_blank</code> should be preferred for textual choices, and <code>allow_null</code> should be preferred for numeric or other non-textual choices.</p>
|
||||
<h2 id="multiplechoicefield">MultipleChoiceField</h2>
|
||||
<p>A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. <code>to_internal_representation</code> returns a <code>set</code> containing the selected values.</p>
|
||||
<p>A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. <code>to_internal_value</code> returns a <code>set</code> containing the selected values.</p>
|
||||
<p><strong>Signature:</strong> <code>MultipleChoiceField(choices)</code></p>
|
||||
<ul>
|
||||
<li><code>choices</code> - A list of valid values, or a list of <code>(key, display_name)</code> tuples.</li>
|
||||
|
@ -804,7 +816,7 @@ Django's regular <a href="https://docs.djangoproject.com/en/dev/ref/settings/#st
|
|||
<p>A field class that validates a list of objects.</p>
|
||||
<p><strong>Signature</strong>: <code>ListField(child)</code></p>
|
||||
<ul>
|
||||
<li><code>child</code> - A field instance that should be used for validating the objects in the list.</li>
|
||||
<li><code>child</code> - A field instance that should be used for validating the objects in the list. If this argument is not provided then objects in the list will not be validated.</li>
|
||||
</ul>
|
||||
<p>For example, to validate a list of integers you might use something like the following:</p>
|
||||
<pre><code>scores = serializers.ListField(
|
||||
|
@ -816,6 +828,19 @@ Django's regular <a href="https://docs.djangoproject.com/en/dev/ref/settings/#st
|
|||
child = serializers.CharField()
|
||||
</code></pre>
|
||||
<p>We can now reuse our custom <code>StringListField</code> class throughout our application, without having to provide a <code>child</code> argument to it.</p>
|
||||
<h2 id="dictfield">DictField</h2>
|
||||
<p>A field class that validates a dictionary of objects. The keys in <code>DictField</code> are always assumed to be string values.</p>
|
||||
<p><strong>Signature</strong>: <code>DictField(child)</code></p>
|
||||
<ul>
|
||||
<li><code>child</code> - A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated.</li>
|
||||
</ul>
|
||||
<p>For example, to create a field that validates a mapping of strings to strings, you would write something like this:</p>
|
||||
<pre><code>document = DictField(child=CharField())
|
||||
</code></pre>
|
||||
<p>You can also use the declarative style, as with <code>ListField</code>. For example:</p>
|
||||
<pre><code>class DocumentField(DictField):
|
||||
child = CharField()
|
||||
</code></pre>
|
||||
<hr />
|
||||
<h1 id="miscellaneous-fields">Miscellaneous fields</h1>
|
||||
<h2 id="readonlyfield">ReadOnlyField</h2>
|
||||
|
@ -845,7 +870,7 @@ Django's regular <a href="https://docs.djangoproject.com/en/dev/ref/settings/#st
|
|||
<p>This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object.</p>
|
||||
<p><strong>Signature</strong>: <code>SerializerMethodField(method_name=None)</code></p>
|
||||
<ul>
|
||||
<li><code>method-name</code> - The name of the method on the serializer to be called. If not included this defaults to <code>get_<field_name></code>.</li>
|
||||
<li><code>method_name</code> - The name of the method on the serializer to be called. If not included this defaults to <code>get_<field_name></code>.</li>
|
||||
</ul>
|
||||
<p>The serializer method referred to by the <code>method_name</code> argument should accept a single argument (in addition to <code>self</code>), which is the object being serialized. It should return whatever you want to be included in the serialized representation of the object. For example:</p>
|
||||
<pre><code>from django.contrib.auth.models import User
|
||||
|
|
|
@ -434,7 +434,7 @@
|
|||
|
||||
|
||||
<li>
|
||||
<a href="#django-rest-framework-chain">Django REST framework chain</a>
|
||||
<a href="#django-rest-framework-filters-package">Django REST framework filters package</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
@ -757,8 +757,8 @@ class ProductFilter(django_filters.FilterSet):
|
|||
<p>We could achieve the same behavior by overriding <code>get_queryset()</code> on the views, but using a filter backend allows you to more easily add this restriction to multiple views, or to apply it across the entire API.</p>
|
||||
<h1 id="third-party-packages">Third party packages</h1>
|
||||
<p>The following third party packages provide additional filter implementations.</p>
|
||||
<h2 id="django-rest-framework-chain">Django REST framework chain</h2>
|
||||
<p>The <a href="https://github.com/philipn/django-rest-framework-chain">django-rest-framework-chain package</a> works together with the <code>DjangoFilterBackend</code> class, and allows you to easily create filters across relationships, or create multiple filter lookup types for a given field.</p>
|
||||
<h2 id="django-rest-framework-filters-package">Django REST framework filters package</h2>
|
||||
<p>The <a href="https://github.com/philipn/django-rest-framework-filters">django-rest-framework-filters package</a> works together with the <code>DjangoFilterBackend</code> class, and allows you to easily create filters across relationships, or create multiple filter lookup types for a given field.</p>
|
||||
|
||||
</div>
|
||||
<!--/span-->
|
||||
|
|
|
@ -483,20 +483,20 @@ router.register(r'users', UserViewSet)
|
|||
router.register(r'accounts', AccountViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
||||
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||
]
|
||||
|
||||
urlpatterns += router.urls
|
||||
</code></pre>
|
||||
<p>Alternatively you can use Django's <code>include</code> function, like so…</p>
|
||||
<pre><code>urlpatterns = [
|
||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
||||
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||
url(r'^', include(router.urls))
|
||||
]
|
||||
</code></pre>
|
||||
<p>Router URL patterns can also be namespaces.</p>
|
||||
<pre><code>urlpatterns = [
|
||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
||||
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||
url(r'^api/', include(router.urls, namespace='api'))
|
||||
]
|
||||
</code></pre>
|
||||
|
|
|
@ -548,7 +548,7 @@ class UserViewSet(viewsets.ModelViewSet):
|
|||
def set_password(self, request, pk=None):
|
||||
...
|
||||
</code></pre>
|
||||
<p>Theses decorators will route <code>GET</code> requests by default, but may also accept other HTTP methods, by using the <code>methods</code> argument. For example:</p>
|
||||
<p>These decorators will route <code>GET</code> requests by default, but may also accept other HTTP methods, by using the <code>methods</code> argument. For example:</p>
|
||||
<pre><code> @detail_route(methods=['post', 'delete'])
|
||||
def unset_password(self, request, pk=None):
|
||||
...
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 11 KiB |
BIN
img/sponsors/2-rheinwerk_verlag.png
Normal file
BIN
img/sponsors/2-rheinwerk_verlag.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -509,7 +509,7 @@
|
|||
<li><a href="http://pypi.python.org/pypi/Markdown/">Markdown</a> (2.1.0+) - Markdown support for the browsable API.</li>
|
||||
<li><a href="http://pypi.python.org/pypi/PyYAML">PyYAML</a> (3.10+) - YAML content-type support.</li>
|
||||
<li><a href="https://pypi.python.org/pypi/defusedxml">defusedxml</a> (0.3+) - XML content-type support.</li>
|
||||
<li><a href="http://pypi.python.org/pypi/django-filter">django-filter</a> (0.5.4+) - Filtering support.</li>
|
||||
<li><a href="http://pypi.python.org/pypi/django-filter">django-filter</a> (0.9.2+) - Filtering support.</li>
|
||||
<li><a href="https://bitbucket.org/david/django-oauth-plus/wiki/Home">django-oauth-plus</a> (2.0+) and <a href="https://github.com/simplegeo/python-oauth2">oauth2</a> (1.5.211+) - OAuth 1.0a support.</li>
|
||||
<li><a href="https://github.com/caffeinehit/django-oauth2-provider">django-oauth2-provider</a> (0.2.3+) - OAuth 2.0 support.</li>
|
||||
<li><a href="https://github.com/lukaszb/django-guardian">django-guardian</a> (1.1.1+) - Object level permissions support.</li>
|
||||
|
|
|
@ -445,7 +445,7 @@
|
|||
<li><a href="http://pulsecode.ca" rel="nofollow" style="background-image:url(../../img/sponsors/2-pulsecode.png);">Pulsecode Inc.</a></li>
|
||||
<li><a href="http://singinghorsestudio.com" rel="nofollow" style="background-image:url(../../img/sponsors/2-singing-horse.png);">Singing Horse Studio Ltd.</a></li>
|
||||
<li><a href="https://www.heroku.com/" rel="nofollow" style="background-image:url(../../img/sponsors/2-heroku.png);">Heroku</a></li>
|
||||
<li><a href="https://www.galileo-press.de/" rel="nofollow" style="background-image:url(../../img/sponsors/2-galileo_press.png);">Galileo Press</a></li>
|
||||
<li><a href="https://www.rheinwerk-verlag.de/" rel="nofollow" style="background-image:url(../../img/sponsors/2-rheinwerk_verlag.png);">Rheinwerk Verlag</a></li>
|
||||
<li><a href="http://www.securitycompass.com/" rel="nofollow" style="background-image:url(../../img/sponsors/2-security_compass.png);">Security Compass</a></li>
|
||||
<li><a href="https://www.djangoproject.com/foundation/" rel="nofollow" style="background-image:url(../../img/sponsors/2-django.png);">Django Software Foundation</a></li>
|
||||
<li><a href="http://www.hipflaskapp.com" rel="nofollow" style="background-image:url(../../img/sponsors/2-hipflask.png);">Hipflask</a></li>
|
||||
|
|
|
@ -439,6 +439,23 @@
|
|||
</code></pre>
|
||||
<hr />
|
||||
<h2 id="30x-series">3.0.x series</h2>
|
||||
<h3 id="304">3.0.4</h3>
|
||||
<p><strong>Date</strong>: <a href="https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.4+Release%22">28th January 2015</a>.</p>
|
||||
<ul>
|
||||
<li>Django 1.8a1 support. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2425">#2425</a>, <a href="https://github.com/tomchristie/django-rest-framework/issues/2446">#2446</a>, <a href="https://github.com/tomchristie/django-rest-framework/issues/2441">#2441</a>)</li>
|
||||
<li>Add <code>DictField</code> and support Django 1.8 <code>HStoreField</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2451">#2451</a>, <a href="https://github.com/tomchristie/django-rest-framework/issues/2106">#2106</a>)</li>
|
||||
<li>Add <code>UUIDField</code> and support Django 1.8 <code>UUIDField</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2448">#2448</a>, <a href="https://github.com/tomchristie/django-rest-framework/issues/2433">#2433</a>, <a href="https://github.com/tomchristie/django-rest-framework/issues/2432">#2432</a>)</li>
|
||||
<li><code>BaseRenderer.render</code> now raises <code>NotImplementedError</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2434">#2434</a>)</li>
|
||||
<li>Fix timedelta JSON serialization on Python 2.6. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2430">#2430</a>)</li>
|
||||
<li><code>ResultDict</code> and <code>ResultList</code> now appear as standard dict/list. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2421">#2421</a>)</li>
|
||||
<li>Fix visible <code>HiddenField</code> in the HTML form of the web browsable API page. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2410">#2410</a>)</li>
|
||||
<li>Use <code>OrderedDict</code> for <code>RelatedField.choices</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2408">#2408</a>)</li>
|
||||
<li>Fix ident format when using <code>HTTP_X_FORWARDED_FOR</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2401">#2401</a>)</li>
|
||||
<li>Fix invalid key with memcached while using throttling. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2400">#2400</a>)</li>
|
||||
<li>Fix <code>FileUploadParser</code> with version 3.x. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2399">#2399</a>)</li>
|
||||
<li>Fix the serializer inheritance. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2388">#2388</a>)</li>
|
||||
<li>Fix caching issues with <code>ReturnDict</code>. (<a href="https://github.com/tomchristie/django-rest-framework/issues/2360">#2360</a>)</li>
|
||||
</ul>
|
||||
<h3 id="303">3.0.3</h3>
|
||||
<p><strong>Date</strong>: <a href="https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.3+Release%22">8th January 2015</a>.</p>
|
||||
<ul>
|
||||
|
@ -1003,7 +1020,8 @@
|
|||
<!-- 3.0.1 -->
|
||||
|
||||
<p><!-- 3.0.2 -->
|
||||
<!-- 3.0.3 --></p>
|
||||
<!-- 3.0.3 -->
|
||||
<!-- 3.0.4 --></p>
|
||||
|
||||
</div>
|
||||
<!--/span-->
|
||||
|
|
|
@ -565,13 +565,13 @@ serializer.data
|
|||
<p>Our <code>SnippetSerializer</code> class is replicating a lot of information that's also contained in the <code>Snippet</code> model. It would be nice if we could keep our code a bit more concise.</p>
|
||||
<p>In the same way that Django provides both <code>Form</code> classes and <code>ModelForm</code> classes, REST framework includes both <code>Serializer</code> classes, and <code>ModelSerializer</code> classes.</p>
|
||||
<p>Let's look at refactoring our serializer using the <code>ModelSerializer</code> class.
|
||||
Open the file <code>snippets/serializers.py</code> again, and edit the <code>SnippetSerializer</code> class.</p>
|
||||
Open the file <code>snippets/serializers.py</code> again, and replace the <code>SnippetSerializer</code> class with the following.</p>
|
||||
<pre><code>class SnippetSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Snippet
|
||||
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
|
||||
</code></pre>
|
||||
<p>One nice property that serializers have is that you can inspect all the fields in a serializer instance, by printing it's representation. Open the Django shell with <code>python manage.py shell</code>, then try the following:</p>
|
||||
<p>One nice property that serializers have is that you can inspect all the fields in a serializer instance, by printing its representation. Open the Django shell with <code>python manage.py shell</code>, then try the following:</p>
|
||||
<pre><code>>>> from snippets.serializers import SnippetSerializer
|
||||
>>> serializer = SnippetSerializer()
|
||||
>>> print(repr(serializer))
|
||||
|
|
|
@ -441,7 +441,7 @@ class SnippetList(APIView):
|
|||
</code></pre>
|
||||
<p>That's looking good. Again, it's still pretty similar to the function based view right now.</p>
|
||||
<p>We'll also need to refactor our <code>urls.py</code> slightly now we're using class based views.</p>
|
||||
<pre><code>from django.conf.urls import patterns, url
|
||||
<pre><code>from django.conf.urls import url
|
||||
from rest_framework.urlpatterns import format_suffix_patterns
|
||||
from snippets import views
|
||||
|
||||
|
|
|
@ -543,7 +543,7 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
|
|||
# Write permissions are only allowed to the owner of the snippet.
|
||||
return obj.owner == request.user
|
||||
</code></pre>
|
||||
<p>Now we can add that custom permission to our snippet instance endpoint, by editing the <code>permission_classes</code> property on the <code>SnippetDetail</code> class:</p>
|
||||
<p>Now we can add that custom permission to our snippet instance endpoint, by editing the <code>permission_classes</code> property on the <code>SnippetDetail</code> view class:</p>
|
||||
<pre><code>permission_classes = (permissions.IsAuthenticatedOrReadOnly,
|
||||
IsOwnerOrReadOnly,)
|
||||
</code></pre>
|
||||
|
|
|
@ -482,7 +482,9 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
|
|||
<li>Our snippet and user serializers include <code>'url'</code> fields that by default will refer to <code>'{model_name}-detail'</code>, which in this case will be <code>'snippet-detail'</code> and <code>'user-detail'</code>.</li>
|
||||
</ul>
|
||||
<p>After adding all those names into our URLconf, our final <code>snippets/urls.py</code> file should look something like this:</p>
|
||||
<pre><code># API endpoints
|
||||
<pre><code>from django.conf.urls import url, include
|
||||
|
||||
# API endpoints
|
||||
urlpatterns = format_suffix_patterns([
|
||||
url(r'^$', views.api_root),
|
||||
url(r'^snippets/$',
|
||||
|
|
Loading…
Reference in New Issue
Block a user