Getting Started with Django-Tastypie
Installation
I’m going to assume that you have already got a Django application that you want to add REST support to.
Firstly, install the requisite python package:
sudo pip install django-tastypie
or, if like me you are using a fedora based machine:
sudo python-pip install django-tastypie
Next, in the settings for your application add an entry for tastypie in the INSTALLED_APPS section. e.g.:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'tastypie',
)
Now, generate the database portions of the tastypie installation:
# python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table tastypie_apiaccess
Creating table tastypie_apikey
Installing custom SQL ...
Installing indexes ...
No fixtures found.
That’s it. You’re ready to add all of your lovely RESTful methods for your objects.
Example of Use
To keep things simple, we’ll assume that we are building a Todo list based system. We already have a basic model that we want to be able to perform RESTful operations on:
from django.db import models
class List(models.Model):
name = models.CharField(max_length = 50)
def __unicode__(self):
return self.name
class ListItem(models.Model):
list_id = models.ForeignKey(List)
item = models.CharField(max_length = 100)
def __unicode__(self):
return self.item
To enable access to these models, we need only create an api.py file with the contents:
from tastypie.resources import ModelResource
from tastypie import fields
from app.models import List, ListItem
class ListResource(ModelResource):
items = fields.ToManyField('app.api.ListItemResource',
'listitems', full=True)
class Meta:
queryset = List.objects.all()
class ListItemResource(ModelResource):
class Meta:
queryset = ListItem.objects.all()
and the (obviously) add some information to our url.py
from django.conf.urls.defaults import *
from tastypie.api import Api
from myapp.api import ListResource, ListItemResource
v1_api = Api(api_name='v1')
v1_api.register(ListResource())
v1_api.register(ListItemResource())
urlpatterns = patterns('',
(r'^lists/', include('myapp.urls')),
(r'^api/', include(v1_api.urls)),
Once done, we should be able to use our web browser to surf to the following urls:
- http://localhost:8000/api/v1/list/?format=json
- http://localhost:8000/api/v1/?format=json
- http://localhost:8000/api/v1/listitem/?format=json
Notice that we’ve had to append “?format=json” to everything, well that’s because we have installed the handlers for other types of representation such as XML or YAML. This message is specifically because we haven’t installed a handler for HTML - which is obviously the default representation requested by a web browser!
If you need these other handlers, the you need to install one of the following:
Another thing to point out at this point is that the REST API is currently limited to read only access. i.e. only GET requests will work. Any attempt to POST, PUT or DELETE data will fail with an HTTP 401 “Unauthoried” error.
We’ll look at that in another post though.
Tweet |
|