Deserialization is similar. First we parse a stream into Python native datatypes...
Deserialization is similar. First we parse a stream into Python native datatypes...
# This import will use either `StringIO.StringIO` or `io.BytesIO`
# This import will use either `StringIO.StringIO` or `io.BytesIO`
# as appropriate, depending on if we're running Python 2 or Python 3.
# as appropriate, depending on if we're running Python 2 or Python 3.
...
@@ -196,7 +196,7 @@ Deserialization is similar. First we parse a stream into Python native datatype
...
@@ -196,7 +196,7 @@ Deserialization is similar. First we parse a stream into Python native datatype
# True
# True
serializer.object
serializer.object
# <Snippet: Snippet object>
# <Snippet: Snippet object>
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.
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.
We can also serialize querysets instead of model instances. To do so we simply add a `many=True` flag to the serializer arguments.
We can also serialize querysets instead of model instances. To do so we simply add a `many=True` flag to the serializer arguments.
...
@@ -264,7 +264,7 @@ The root of our API is going to be a view that supports listing all the existing
...
@@ -264,7 +264,7 @@ The root of our API is going to be a view that supports listing all the existing
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.
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 corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
...
@@ -277,11 +277,11 @@ We'll also need a view which corresponds to an individual snippet, and can be us
...
@@ -277,11 +277,11 @@ We'll also need a view which corresponds to an individual snippet, and can be us
@@ -33,7 +33,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r
...
@@ -33,7 +33,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r
## Pulling it all together
## Pulling it all together
Okay, let's go ahead and start using these new components to write a few views.
Okay, let's go ahead and start using these new components to write a few views.
We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
...
@@ -69,7 +69,7 @@ Here is the view for an individual snippet, in the `views.py` module.
...
@@ -69,7 +69,7 @@ Here is the view for an individual snippet, in the `views.py` module.
def snippet_detail(request, pk):
def snippet_detail(request, pk):
"""
"""
Retrieve, update or delete a snippet instance.
Retrieve, update or delete a snippet instance.
"""
"""
try:
try:
snippet = Snippet.objects.get(pk=pk)
snippet = Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
except Snippet.DoesNotExist:
...
@@ -115,7 +115,7 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
...
@@ -115,7 +115,7 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`.
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`.
class SnippetDetail(APIView):
class SnippetDetail(APIView):
"""
"""
...
@@ -72,7 +72,7 @@ We'll also need to refactor our `urls.py` slightly now we're using class based v
...
@@ -72,7 +72,7 @@ We'll also need to refactor our `urls.py` slightly now we're using class based v
At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our API, by instead using hyperlinking for relationships.
At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our API, by instead using hyperlinking for relationships.
## Creating an endpoint for the root of our API
## Creating an endpoint for the root of our API
...
@@ -37,7 +37,7 @@ Instead of using a concrete generic view, we'll use the base class for represent
...
@@ -37,7 +37,7 @@ Instead of using a concrete generic view, we'll use the base class for represent