What's new? | Help | Directory | Sign in
Google
django-rest-interface
A generic REST interface for Django
  
  
  
  
    
Search
for
Updated Aug 13, 2007 by stuhlmueller
Labels: Featured
RestifyDjango  
How to provide a REST API for an existing Django application using the Django REST interface

1. Design the interface

Decide, which resources you want to make available and in what way.

Resources are sources of specific information, each of which can be referred to using a global identifier (a URI). In order to manipulate these resources, components of the network (clients and servers) communicate via a standardized interface (e.g. HTTP) and exchange representations of these resources (the actual documents conveying the information). Wikipedia: REST

Usually, this involves four steps:

  1. What are the URIs? (Resources)
  2. What's the format? (Representation)
  3. What methods are supported at each URI? (Access restriction)
  4. (What status codes could be returned?)

Some resources may directly correspond to Django models, others may not involve model data at all (e.g. an index page for service discovery) or may correspond to more than one model (e.g. a "marriage" resource that corresponds to a "married_to" one-to-one relationship between two user models). For a good example of how to identify the resources of a complex application, see Restify DayTrader.

2. Create model-based resources

For every model you want to be part of the API, create a Collection instance and add it to urlpatterns in urls.py:

from django_restapi.model_resource import Collection
from django_restapi.responder import XMLResponder
from django_restapi_tests.polls.models import Poll, Choice

xml_poll_resource = Collection(
    queryset = Poll.objects.all(),
    permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),
    responder = XMLResponder(paginate_by = 10)
)

urlpatterns = patterns('',
   # ...
   url(r'^xml/polls/(.*?)/?$', xml_poll_resource),
)

See more elaborate examples.

3. Create non-model-based resources

Subclass Resource, overwrite some or all of the create/read/update/delete methods and, in some cases, get_url, and add it to urlpatterns in urls.py:

from django_restapi.resource import Resource

class MyResource(Resource):
    def read(self, request):
        # ...
    def update(self, request):
        # ...

urlpatterns = patterns('',
   # ...
   url(r'^my_resource/$', MyResource()),
)

See an example.


Comment by gust...@fundacaoaprender.org.br, Dec 20, 2007

Hi All,

I found an error in example #3. The create/read/update/delete signatures need args and kwargs arguments to work.

class MyResource(Resource):
    def read(self, request, *args, **kwargs):
        # ...
    def update(self, request, *args, **kwargs):
        # ...

Sign in to add a comment