Home » Django » Django and Restful APIs

Django and Restful APIs

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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/

Questions:
Answers:

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)

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

Have a look at this RestifyDjango.

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

Questions:
Answers:

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

Savory Pie is a REST framework that supports django.

Questions:
Answers:

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.