django-rest-framework/docs/tutorial/1-serialization.md

308 lines
12 KiB
Markdown
Raw Normal View History

2012-08-29 23:57:37 +04:00
# Tutorial 1: Serialization
## Introduction
2012-10-28 23:25:51 +04:00
This tutorial will cover creating a simple pastebin code highlighting Web API. Along the way it will introduce the various components that make up REST framework, and give you a comprehensive understanding of how everything fits together.
The tutorial is fairly in-depth, so you should probably get a cookie and a cup of your favorite brew before getting started. If you just want a quick overview, you should head over to the [quickstart] documentation instead.
2012-08-29 23:57:37 +04:00
2012-09-03 16:10:39 +04:00
## Setting up a new environment
2012-08-29 23:57:37 +04:00
2012-09-03 15:41:52 +04:00
Before we do anything else we'll create a new virtual environment, using [virtualenv]. This will make sure our package configuration is keep nicely isolated from any other projects we're working on.
2012-09-08 23:23:32 +04:00
:::bash
2012-09-03 16:10:39 +04:00
mkdir ~/env
virtualenv ~/env/tutorial
2012-09-03 16:44:49 +04:00
source ~/env/tutorial/bin/activate
2012-09-03 15:41:52 +04:00
2012-09-03 16:10:39 +04:00
Now that we're inside a virtualenv environment, we can install our package requirements.
2012-09-03 15:41:52 +04:00
pip install django
pip install djangorestframework
2012-10-28 23:25:51 +04:00
pip install pygments # We'll be using this for the code highlighting
2012-09-03 15:41:52 +04:00
2012-09-03 16:30:20 +04:00
**Note:** To exit the virtualenv environment at any time, just type `deactivate`. For more information see the [virtualenv documentation][virtualenv].
2012-09-03 16:10:39 +04:00
## Getting started
Okay, we're ready to get coding.
2012-08-29 23:57:37 +04:00
To get started, let's create a new project to work with.
2012-10-05 18:22:30 +04:00
cd ~
2012-08-29 23:57:37 +04:00
django-admin.py startproject tutorial
cd tutorial
Once that's done we can create an app that we'll use to create a simple Web API.
2012-10-28 23:25:51 +04:00
We're going to create a project that
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
python manage.py startapp snippets
2012-08-29 23:57:37 +04:00
The simplest way to get up and running will probably be to use an `sqlite3` database for the tutorial. Edit the `tutorial/settings.py` file, and set the default database `"ENGINE"` to `"sqlite3"`, and `"NAME"` to `"tmp.db"`.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'tmp.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
2012-10-28 23:25:51 +04:00
We'll also need to add our new `snippets` app and the `rest_framework` app to `INSTALLED_APPS`.
2012-08-29 23:57:37 +04:00
INSTALLED_APPS = (
...
'rest_framework',
2012-10-28 23:25:51 +04:00
'snippets'
2012-08-29 23:57:37 +04:00
)
2012-10-28 23:25:51 +04:00
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet views.
2012-08-29 23:57:37 +04:00
urlpatterns = patterns('',
2012-10-28 23:25:51 +04:00
url(r'^', include('snippets.urls')),
2012-08-29 23:57:37 +04:00
)
Okay, we're ready to roll.
## Creating a model to work with
2012-10-28 23:25:51 +04:00
For the purposes of this tutorial we're going to start by creating a simple `Snippet` model that is used to store code snippets. Go ahead and edit the `snippets` app's `models.py` file.
2012-08-29 23:57:37 +04:00
from django.db import models
2012-10-28 23:25:51 +04:00
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in get_all_lexers()])
STYLE_CHOICES = sorted((item, item) for item in list(get_all_styles()))
class Snippet(models.Model):
2012-08-29 23:57:37 +04:00
created = models.DateTimeField(auto_now_add=True)
2012-10-28 23:25:51 +04:00
title = models.CharField(max_length=100, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES,
default='python',
max_length=100)
style = models.CharField(choices=STYLE_CHOICES,
2012-10-29 11:54:14 +04:00
default='friendly',
max_length=100)
2012-10-28 23:25:51 +04:00
class Meta:
ordering = ('created',)
2012-08-29 23:57:37 +04:00
Don't forget to sync the database for the first time.
python manage.py syncdb
## Creating a Serializer class
2012-10-28 23:25:51 +04:00
The first thing we need to get started on our Web API is provide a way of serializing and deserializing the snippet instances into representations such as `json`. We can do this by declaring serializers that work very similarly to Django's forms. Create a file in the `snippets` directory named `serializers.py` and add the following.
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
from django.forms import widgets
from rest_framework import serializers
2012-10-28 23:25:51 +04:00
from snippets import models
class SnippetSerializer(serializers.Serializer):
pk = serializers.Field() # Note: `Field` is an untyped read-only field.
title = serializers.CharField(required=False,
max_length=100)
code = serializers.CharField(widget=widgets.Textarea,
max_length=100000)
linenos = serializers.BooleanField(required=False)
language = serializers.ChoiceField(choices=models.LANGUAGE_CHOICES,
default='python')
style = serializers.ChoiceField(choices=models.STYLE_CHOICES,
default='friendly')
2012-08-29 23:57:37 +04:00
def restore_object(self, attrs, instance=None):
"""
2012-10-28 23:25:51 +04:00
Create or update a new snippet instance.
2012-08-29 23:57:37 +04:00
"""
if instance:
2012-10-28 23:25:51 +04:00
# Update existing instance
instance.title = attrs['title']
instance.code = attrs['code']
instance.linenos = attrs['linenos']
instance.language = attrs['language']
instance.style = attrs['style']
2012-08-29 23:57:37 +04:00
return instance
2012-10-28 23:25:51 +04:00
# Create new instance
return models.Snippet(**attrs)
2012-08-29 23:57:37 +04:00
The first part of serializer class defines the fields that get serialized/deserialized. The `restore_object` method defines how fully fledged instances get created when deserializing data.
We can actually also save ourselves some time by using the `ModelSerializer` class, as we'll see later, but for now we'll keep our serializer definition explicit.
## Working with Serializers
Before we go any further we'll familiarise ourselves with using our new Serializer class. Let's drop into the Django shell.
python manage.py shell
2012-10-28 23:25:51 +04:00
Okay, once we've got a few imports out of the way, let's create a code snippet to work with.
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
snippet = Snippet(code='print "hello, world"\n')
snippet.save()
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
We've now got a few snippet instances to play with. Let's take a look at serializing one of those instances.
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
serializer = SnippetSerializer(instance=snippet)
2012-08-29 23:57:37 +04:00
serializer.data
2012-10-28 23:25:51 +04:00
# {'pk': 1, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'}
2012-08-29 23:57:37 +04:00
At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`.
content = JSONRenderer().render(serializer.data)
content
2012-10-28 23:25:51 +04:00
# '{"pk": 1, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
2012-08-29 23:57:37 +04:00
Deserialization is similar. First we parse a stream into python native datatypes...
import StringIO
stream = StringIO.StringIO(content)
2012-08-29 23:57:37 +04:00
data = JSONParser().parse(stream)
...then we restore those native datatypes into to a fully populated object instance.
2012-10-28 23:25:51 +04:00
serializer = SnippetSerializer(data)
2012-08-29 23:57:37 +04:00
serializer.is_valid()
# True
serializer.object
2012-10-28 23:25:51 +04:00
# <Snippet: Snippet object>
2012-08-29 23:57:37 +04:00
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.
2012-10-28 23:25:51 +04:00
## Using ModelSerializers
Our `SnippetSerializer` class is replicating a lot of information that's also contained in the `Snippet` model. It would be nice if we could keep out code a bit more concise.
In the same way that Django provides both `Form` classes and `ModelForm` classes, REST framework includes both `Serializer` classes, and `ModelSerializer` classes.
Let's look at refactoring our serializer using the `ModelSerializer` class.
Open the file `snippets/serializers.py` again, and edit the `SnippetSerializer` class.
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = models.Snippet
fields = ('pk', 'title', 'code', 'linenos', 'language', 'style')
## Writing regular Django views using our Serializer
2012-08-29 23:57:37 +04:00
Let's see how we can write some API views using our new Serializer class.
2012-10-28 23:25:51 +04:00
For the moment we won't use any of REST framework's other features, we'll just write the views as regular Django views.
2012-08-29 23:57:37 +04:00
We'll start off by creating a subclass of HttpResponse that we can use to render any data we return into `json`.
2012-10-28 23:25:51 +04:00
Edit the `snippet/views.py` file, and add the following.
2012-08-29 23:57:37 +04:00
2012-09-25 15:27:46 +04:00
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
2012-10-28 23:25:51 +04:00
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
2012-08-29 23:57:37 +04:00
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders it's content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
2012-10-28 23:25:51 +04:00
The root of our API is going to be a view that supports listing all the existing snippets, or creating a new snippet.
2012-08-29 23:57:37 +04:00
2012-09-25 15:27:46 +04:00
@csrf_exempt
2012-10-28 23:25:51 +04:00
def snippet_list(request):
2012-08-29 23:57:37 +04:00
"""
2012-10-28 23:25:51 +04:00
List all code snippets, or create a new snippet.
2012-08-29 23:57:37 +04:00
"""
if request.method == 'GET':
2012-10-28 23:25:51 +04:00
snippets = Snippet.objects.all()
serializer = SnippetSerializer(instance=snippets)
2012-08-29 23:57:37 +04:00
return JSONResponse(serializer.data)
elif request.method == 'POST':
data = JSONParser().parse(request)
2012-10-28 23:25:51 +04:00
serializer = SnippetSerializer(data)
2012-08-29 23:57:37 +04:00
if serializer.is_valid():
2012-10-28 23:25:51 +04:00
serializer.save()
2012-08-29 23:57:37 +04:00
return JSONResponse(serializer.data, status=201)
else:
2012-09-17 23:19:45 +04:00
return JSONResponse(serializer.errors, status=400)
2012-08-29 23:57:37 +04:00
2012-09-25 15:27:46 +04:00
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.
2012-10-28 23:25:51 +04:00
We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
2012-08-29 23:57:37 +04:00
2012-09-25 15:27:46 +04:00
@csrf_exempt
2012-10-28 23:25:51 +04:00
def snippet_detail(request, pk):
2012-08-29 23:57:37 +04:00
"""
2012-10-28 23:25:51 +04:00
Retrieve, update or delete a code snippet.
2012-08-29 23:57:37 +04:00
"""
try:
2012-10-28 23:25:51 +04:00
snippet = Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
2012-08-29 23:57:37 +04:00
return HttpResponse(status=404)
if request.method == 'GET':
2012-10-28 23:25:51 +04:00
serializer = SnippetSerializer(instance=snippet)
2012-08-29 23:57:37 +04:00
return JSONResponse(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
2012-10-28 23:25:51 +04:00
serializer = SnippetSerializer(data, instance=snippet)
2012-08-29 23:57:37 +04:00
if serializer.is_valid():
2012-10-28 23:25:51 +04:00
serializer.save()
2012-08-29 23:57:37 +04:00
return JSONResponse(serializer.data)
else:
2012-09-17 23:19:45 +04:00
return JSONResponse(serializer.errors, status=400)
2012-08-29 23:57:37 +04:00
elif request.method == 'DELETE':
2012-10-28 23:25:51 +04:00
snippet.delete()
2012-08-29 23:57:37 +04:00
return HttpResponse(status=204)
2012-10-28 23:37:27 +04:00
Finally we need to wire these views up. Create the `snippets/urls.py` file:
2012-08-29 23:57:37 +04:00
from django.conf.urls import patterns, url
2012-10-28 23:25:51 +04:00
urlpatterns = patterns('snippets.views',
2012-10-28 23:37:27 +04:00
url(r'^snippets/$', 'snippet_list'),
url(r'^snippets/(?P<pk>[0-9]+)/$', 'snippet_detail')
2012-08-29 23:57:37 +04:00
)
It's worth noting that there's a couple of edge cases we're not dealing with properly at the moment. If we send malformed `json`, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.
## Testing our first attempt at a Web API
**TODO: Describe using runserver and making example requests from console**
**TODO: Describe opening in a web browser and viewing json output**
## Where are we now
We're doing okay so far, we've got a serialization API that feels pretty similar to Django's Forms API, and some regular Django views.
Our API views don't do anything particularly special at the moment, beyond serve `json` responses, and there's some error handling edge cases we'd still like to clean up, but it's a functioning Web API.
2012-09-03 15:41:52 +04:00
We'll see how we can start to improve things in [part 2 of the tutorial][tut-2].
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
[quickstart]: quickstart.md
2012-09-03 15:41:52 +04:00
[virtualenv]: http://www.virtualenv.org/en/latest/index.html
[tut-2]: 2-requests-and-responses.md