My favorites | Sign in
Project Logo
                
Blogs:
Feeds:
People details
Project owners:
  asmith607

Update

As I explained in this blog post, I am suspending work on this project to support the work of Andreas Stuhlmueller who is going to be implementing a more comprehensive RESTful API for Django as part of the Google Summer of Code. As his work looks like the most promising implementation, and many of his ideas are compatible with mine, I would prefer to spend my time supporting him by providing feedback, testing releases, possibly contributing documentation, etc. rather than developing partially overlapping code.

If you are interested, here are some relevant links to Andreas' proposal:

Background

As someone who has been away from Python for a little while and is new to Django and to the REST philosophy, I only set out to better familiarize myself with Django and to better understand REST, not to create this contribution. However, this is where my exploration of Django and REST have taken me. The following blog posts chronicle this exploration and should provide insight into the approach taken here:

At this stage, I offer this contribution only as an attempted proof of concept, and leave it for more experienced Django developers to judge if this approach is sound. I have attempted to follow best practices for implementing pure REST--as much as pure REST can be implemented--and to do so in a way that is compatible with Django. However, at this point, the implemenation is incomplete, and hasn't been tested in a real application environment.

Installation and Setup

Step 1: Checkout from Subversion:

  svn checkout http://django-restful-model-views.googlecode.com/svn/trunk/ restful_model_views

Step 2: Copy or link this directory under django/contrib .

Step 3: In settings.py, separate the applications that should be RESTful:

  RESTFUL_APPS = (
    'mysite.myrestfulapp',
  )

  INSTALLED_APPS = RESTFUL_APPS + (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
  )

Step 4: In urls.py, add RESTful URL patterns:

  from django.contrib.restful_model_views.restful_urls import get_restful_patterns
  urlpatterns += get_restful_patterns()

Step 5: Instead of a single views.py, create a directory called 'views' in your RESTful application, and don't forget to add an empty __init__.py file under it, so Python knows it's a module. Now, for each model class that should have RESTful views, create a new module and class under views based on the name of the model. A minimal implementation for a model class called 'Items' would contain this code in views/Items.py:

from mysite.myrestfulapp.models import Items
from django.contrib.restful_model_views.ModelResources import ModelResources, edit, create, dispatch

class Items(ModelResources):
  pass

  class Items_Member (ModelResources.ModelResources_Member):
    pass

This minimal implementation makes the underlying view methods in Resources available to Django for the model. You can refer to the django.contrib.restful_model_views.Resources module to see a full implemenation, otherwise, the basic structure of a RESTful model view looks like this:

  class Resources
    def GET(request):
      pass
    
    def POST(request):
      pass
    
    class Creator:
    
      def GET(self, request):
        pass
    
    class Resources_Member:
    
      def GET(self, request):
        pass
    
      def PUT(self, request):
        pass
    
      def DELETE(self, request):
        pass
    
      class Editor:
    
        def GET(self, request):
          pass

[TODO: map these methods to form actions] Override any methods you need to customize. Otherwise, the default implemenations are designed to use generic views, so the appropriate templates need to be in the usual places. (Consult the Django generic views documentation if you don't know where these are.) Also note that to overload POST for PUT and DELETE operations, you will have to add a form element called '_method' with the appropriate values 'PUT' or 'DELETE'.

Development Roadmap

This contribution is currently in a 'proof of concept' stage, although there are unit tests distributed with it, so what is there should at least work. Immediate/obvious work to do includes:

The following are discussed in more detail in Evolving a RESTful Django Contribution:

Background Resources

The following are some of the resources that have shaped my thinking and approach to this contribution:

[TODO: add introductory REST links, relevant Django links?]

Acknowledgements

Thank you to those who have taken the time to give valuable feedback so far:

Alternate approaches:

Contact

adam smith









Hosted by Google Code