From f7c338757433763cc464f6948ee70b621da9a0a0 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 25 Sep 2012 13:40:44 +0100 Subject: [PATCH] Latest docs build --- tutorial/1-serialization.html | 17 +++++++++++------ tutorial/3-class-based-views.html | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tutorial/1-serialization.html b/tutorial/1-serialization.html index 2ffaf5126..829fee688 100644 --- a/tutorial/1-serialization.html +++ b/tutorial/1-serialization.html @@ -162,7 +162,7 @@ cd tutorial

For the purposes of this tutorial we're going to start by creating a simple Comment model that is used to store comments against a blog post. Go ahead and edit the blog app's models.py file.

from django.db import models
 
-class Comment(models.Model):
+class Comment(models.Model):        
     email = models.EmailField()
     content = models.CharField(max_length=200)
     created = models.DateTimeField(auto_now_add=True)
@@ -176,6 +176,7 @@ class Comment(models.Model):
 from rest_framework import serializers
 
 class CommentSerializer(serializers.Serializer):
+    id = serializers.IntegerField(readonly=True)
     email = serializers.EmailField()
     content = serializers.CharField(max_length=200)
     created = serializers.DateTimeField()
@@ -214,12 +215,12 @@ c3.save()
 

We've now got a few comment instances to play with. Let's take a look at serializing one of those instances.

serializer = CommentSerializer(instance=c1)
 serializer.data
-# {'email': u'leila@example.com', 'content': u'nothing to say', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774, tzinfo=<UTC>)}
+# {'id': 1, 'email': u'leila@example.com', 'content': u'nothing to say', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774, tzinfo=<UTC>)}
 

At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into json.

stream = JSONRenderer().render(serializer.data)
 stream
-# '{"email": "leila@example.com", "content": "nothing to say", "created": "2012-08-22T16:20:09.822"}'
+# '{"id": 1, "email": "leila@example.com", "content": "nothing to say", "created": "2012-08-22T16:20:09.822"}'
 

Deserialization is similar. First we parse a stream into python native datatypes...

data = JSONParser().parse(stream)
@@ -238,9 +239,10 @@ We'll start off by creating a subclass of HttpResponse that we can use to render
 

Edit the blog/views.py file, and add the following.

from blog.models import Comment
 from blog.serializers import CommentSerializer
+from django.http import HttpResponse
+from django.views.decorators.csrf import csrf_exempt
 from rest_framework.renderers import JSONRenderer
 from rest_framework.parsers import JSONParser
-from django.http import HttpResponse
 
 class JSONResponse(HttpResponse):
     """
@@ -253,7 +255,8 @@ class JSONResponse(HttpResponse):
         super(JSONResponse, self).__init__(content, **kwargs)
 

The root of our API is going to be a view that supports listing all the existing comments, or creating a new comment.

-
def comment_root(request):
+
@csrf_exempt
+def comment_root(request):
     """
     List all comments, or create a new comment.
     """
@@ -272,8 +275,10 @@ class JSONResponse(HttpResponse):
         else:
             return JSONResponse(serializer.errors, status=400)
 
+

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.

We'll also need a view which corrosponds to an individual comment, and can be used to retrieve, update or delete the comment.

-
def comment_instance(request, pk):
+
@csrf_exempt
+def comment_instance(request, pk):
     """
     Retrieve, update or delete a comment instance.
     """
diff --git a/tutorial/3-class-based-views.html b/tutorial/3-class-based-views.html
index 89aa03687..9ba71d04a 100644
--- a/tutorial/3-class-based-views.html
+++ b/tutorial/3-class-based-views.html
@@ -152,7 +152,7 @@ class CommentRoot(APIView):
         comment = self.get_object(pk)
         serializer = CommentSerializer(request.DATA, instance=comment)
         if serializer.is_valid():
-            comment = serializer.deserialized
+            comment = serializer.object
             comment.save()
             return Response(serializer.data)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)