aboutsummaryrefslogtreecommitdiffstats
path: root/tutorial
diff options
context:
space:
mode:
authorTom Christie2014-12-05 09:44:35 +0000
committerTom Christie2014-12-05 09:44:35 +0000
commit6cce0681a9da20f46f57aa2c19796b4266c3d505 (patch)
treeaa29a4123d2de4fbef7d28cc9aa881c44546ed76 /tutorial
parent442f49eb5b8bfe6aebd599191482e2ffc532d87c (diff)
downloaddjango-rest-framework-6cce0681a9da20f46f57aa2c19796b4266c3d505.tar.bz2
Update documentation
Diffstat (limited to 'tutorial')
-rw-r--r--tutorial/1-serialization/index.html24
-rw-r--r--tutorial/6-viewsets-and-routers/index.html4
-rw-r--r--tutorial/quickstart/index.html5
3 files changed, 16 insertions, 17 deletions
diff --git a/tutorial/1-serialization/index.html b/tutorial/1-serialization/index.html
index dda47f72..9b5ffb35 100644
--- a/tutorial/1-serialization/index.html
+++ b/tutorial/1-serialization/index.html
@@ -483,7 +483,7 @@ from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
- title = serializers.CharField(required=False,
+ title = serializers.CharField(required=False, allow_blank=True
max_length=100)
code = serializers.CharField(style={'type': 'textarea'})
linenos = serializers.BooleanField(required=False)
@@ -492,21 +492,21 @@ class SnippetSerializer(serializers.Serializer):
style = serializers.ChoiceField(choices=STYLE_CHOICES,
default='friendly')
- def create(self, validated_attrs):
+ def create(self, validated_data):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
- return Snippet.objects.create(**validated_attrs)
+ return Snippet.objects.create(**validated_data)
- def update(self, instance, validated_attrs):
+ def update(self, instance, validated_data):
"""
Update and return an existing `Snippet` instance, given the validated data.
"""
- instance.title = validated_attrs.get('title', instance.title)
- instance.code = validated_attrs.get('code', instance.code)
- instance.linenos = validated_attrs.get('linenos', instance.linenos)
- instance.language = validated_attrs.get('language', instance.language)
- instance.style = validated_attrs.get('style', instance.style)
+ instance.title = validated_data.get('title', instance.title)
+ instance.code = validated_data.get('code', instance.code)
+ instance.linenos = validated_data.get('linenos', instance.linenos)
+ instance.language = validated_data.get('language', instance.language)
+ instance.style = validated_data.get('style', instance.style)
instance.save()
return instance
</code></pre>
@@ -552,7 +552,7 @@ data = JSONParser().parse(stream)
<pre><code>serializer = SnippetSerializer(data=data)
serializer.is_valid()
# True
-serializer.object
+serializer.save()
# &lt;Snippet: Snippet object&gt;
</code></pre>
<p>Notice how similar the API is to working with forms. The similarity should become even more apparent when we start writing views that use our serializer.</p>
@@ -574,7 +574,7 @@ Open the file <code>snippets/serializers.py</code> again, and edit the <code>Sni
<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 manange.py shell</code>, then try the following:</p>
<pre><code>&gt;&gt;&gt; from snippets.serializers import SnippetSerializer
&gt;&gt;&gt; serializer = SnippetSerializer()
-&gt;&gt;&gt; print repr(serializer) # In python 3 use `print(repr(serializer))`
+&gt;&gt;&gt; print(repr(serializer))
SnippetSerializer():
id = IntegerField(label='ID', read_only=True)
title = CharField(allow_blank=True, max_length=100, required=False)
@@ -657,7 +657,7 @@ def snippet_detail(request, pk):
return HttpResponse(status=204)
</code></pre>
<p>Finally we need to wire these views up. Create the <code>snippets/urls.py</code> file:</p>
-<pre><code>from django.conf.urls import patterns, url
+<pre><code>from django.conf.urls import url
from snippets import views
urlpatterns = [
diff --git a/tutorial/6-viewsets-and-routers/index.html b/tutorial/6-viewsets-and-routers/index.html
index 0740d496..39d53591 100644
--- a/tutorial/6-viewsets-and-routers/index.html
+++ b/tutorial/6-viewsets-and-routers/index.html
@@ -480,7 +480,7 @@ router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
# The API URLs are now determined automatically by the router.
-# Additionally, we include the login URLs for the browseable API.
+# Additionally, we include the login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
@@ -492,7 +492,7 @@ urlpatterns = [
<p>Using viewsets can be a really useful abstraction. It helps ensure that URL conventions will be consistent across your API, minimizes the amount of code you need to write, and allows you to concentrate on the interactions and representations your API provides rather than the specifics of the URL conf.</p>
<p>That doesn't mean it's always the right approach to take. There's a similar set of trade-offs to consider as when using class-based views instead of function based views. Using viewsets is less explicit than building your views individually.</p>
<h2 id="reviewing-our-work">Reviewing our work</h2>
-<p>With an incredibly small amount of code, we've now got a complete pastebin Web API, which is fully web browseable, and comes complete with authentication, per-object permissions, and multiple renderer formats.</p>
+<p>With an incredibly small amount of code, we've now got a complete pastebin Web API, which is fully web browsable, and comes complete with authentication, per-object permissions, and multiple renderer formats.</p>
<p>We've walked through each step of the design process, and seen how if we need to customize anything we can gradually work our way down to simply using regular Django views.</p>
<p>You can review the final <a href="https://github.com/tomchristie/rest-framework-tutorial">tutorial code</a> on GitHub, or try out a live example in <a href="http://restframework.herokuapp.com/">the sandbox</a>.</p>
<h2 id="onwards-and-upwards">Onwards and upwards</h2>
diff --git a/tutorial/quickstart/index.html b/tutorial/quickstart/index.html
index 762c9aeb..f291901c 100644
--- a/tutorial/quickstart/index.html
+++ b/tutorial/quickstart/index.html
@@ -403,10 +403,9 @@ pip install django
pip install djangorestframework
# Set up a new project with a single application
-django-admin.py startproject tutorial
+django-admin.py startproject tutorial .
cd tutorial
django-admin.py startapp quickstart
-cd ..
</code></pre>
<p>Now sync your database for the first time:</p>
<pre><code>python manage.py migrate
@@ -470,7 +469,7 @@ router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
-# Additionally, we include login URLs for the browseable API.
+# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))