58

I have been struggling with choosing a methodology for creating a RESTful API with Django. None of the approaches I've tried seem to be the "silver" bullet. WAPI from http://fi.am is probably the closest to what I would like to accomplish, however I am not sure if it is acceptable in a true RESTful API to have parameters that are resource identifiers be in the querystring instead of in a "clean" URL format. Any suggestions for modifying WAPIs RestBinding.PATTERN to "clean" up the URLs? Another option I've explored is Django-Rest-Interface. However this framework seems to violate one of the most important pieces I need, and that is to include the full resource URL for references to other resources (see http://jacobian.org/writing/rest-worst-practices/ Improper Use of Links). The final option is to use django-multiresponse and basically do it the long way.

Please offer me your best advice, especially people that have dealt with this decision.

gsiegman
  • 1,151
  • 1
  • 10
  • 16

8 Answers8

41

For Django, besides tastypie and piston, django-rest-framework is a promising one worth mentioning. I've already migrated one of my projects on it smoothly.

Django REST framework is a lightweight REST framework for Django, that aims to make it easy to build well-connected, self-describing RESTful Web APIs.

Quick example:

from django.conf.urls.defaults import patterns, url
from djangorestframework.resources import ModelResource
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
from myapp.models import MyModel

class MyResource(ModelResource):
    model = MyModel

urlpatterns = patterns('',
    url(r'^$', ListOrCreateModelView.as_view(resource=MyResource)),
    url(r'^(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MyResource)),
)

Take the example from the official site, all above codes provide api, self explained documentation (like soap based webservice) and even sandboxing for testing. Very convenient.

Links: http://django-rest-framework.org/

jdero
  • 1,797
  • 3
  • 21
  • 36
Sun Liwen
  • 1,224
  • 1
  • 15
  • 21
18

I believe the recently released django-piston is now the best solution for creating a proper REST interface in Django. django-piston

Note: django-piston seems to no longer be maintained (see comments below)

Mr Shark
  • 26,068
  • 5
  • 29
  • 37
gsiegman
  • 1,151
  • 1
  • 10
  • 16
  • 7
    django-piston hasn't been well maintained for years. While it has a new maintainer, better to pick something from http://djangopackages.com/grids/g/api/ that is more current. – pydanny Oct 05 '11 at 07:10
  • 10
    I would *strongly* advise avoiding django-piston. I've been using it since it was abandoned and can make life *very* painful, especially if you need to start doing anything remotely nonstandard with the serializer. – David Wolever Nov 17 '11 at 22:59
  • 8
    django-tastypie is now my preferred solution for REST. – gsiegman Jan 10 '12 at 19:48
  • please add me some reps so that I could vote down. piston is tighed to model and you cannot add 2 handlers referencing the same model. Also, if you have 2 handlers, let's call them Customer and Address handlers. Imagine your address handler is going to show customer name and the adddress. you are doomed to receive all fields, you've got mentioned on Customers handler. A day wasted on the pore technology. – maxk Oct 17 '12 at 20:56
  • 8
    Would be good to update this answer to note that **piston is no longer maintained, and probably shouldn't be used for new builds at this point**. Tastypie and Django REST framework are the most mature, fully featured frameworks, and there are a bunch of smaller projects out there. – Tom Christie Dec 07 '12 at 15:00
9

django-tastypie is a good way to do it, their slogan: "Creating delicious APIs for Django apps since 2010" is pretty comforting ;)

guerrerocarlos
  • 1,374
  • 12
  • 6
5

You could take look at django-dynamicresponse, which is a lightweight framework for adding REST API with JSON to your Django applications.

It requires minimal changes to add API support to existing Django apps, and makes it straight-forward to build-in API from the start in new projects.

Basically, it includes middleware support for parsing JSON into request.POST, in addition to serializing the returned context to JSON or rendering a template/redirecting conditionally based on the request type.

This approach differs from other frameworks (such as django-piston) in that you do not need to create separate handlers for API requests. You can also reuse your existing view logic, and keep using form validation etc. like normal views.

chrismi
  • 71
  • 1
  • 1
4

I don't know if this project can be useful for you, but sending a link can hardly hurt. Take a look at django-apibuilder , available from http://opensource.washingtontimes.com/projects/django-apibuilder/ . Perhaps it can be useful?

/Jesper

0

https://github.com/RueLaLa/savory-pie

Savory Pie is a REST framework that supports django.

Mike Milkin
  • 3,961
  • 4
  • 17
  • 10
0

I would suggest you look into Django Rest Framework (DRF), play around with this and see if it suits your requirements. The reason I recommend DRF is because it makes making the API views really simple with the use of GenericAPIView classes, Mixin Classes and Mixed in Generic views. You can easily make use of tried and tested design patterns for making your API endpoints as well as keeping your code base neat and concise. You also DRY when writing your code which is always great. Your API views are literally 2-3 lines long.

You can checkout this tutorial http://programmathics.com/programming/python/django-rest-framework-setup/ that begins from setting up your environment to going through the different ways to make your RESTful API using the django rest framework.

Disclaimer: I am the creator of that website.

SeekingAlpha
  • 7,489
  • 12
  • 35
  • 44
0

Have a look at this RestifyDjango.

Somewhat related are Django XML-RPC and JSON-RPC.

Soviut
  • 88,194
  • 49
  • 192
  • 260