Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and Restful APIs

Tags:

rest

django

api

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.

like image 925
gsiegman Avatar asked Mar 18 '09 04:03

gsiegman


People also ask

Can Django be used for REST API?

You can create classic web applications via Django and expose their functionality to the world through REST APIs. In fact, this is pretty easy to do! Though, the Django REST Framework is more specialized for this task, is built on top of plain Django and makes the process easier.

What is the difference between Django and REST API?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.

Can I build an API with Django?

Django REST Framework is a wrapper over default Django Framework, basically used to create APIs of various kinds. There are three stages before creating a API through REST framework, Converting a Model's data to JSON/XML format (Serialization), Rendering this data to the view, Creating a URL for mapping to the viewset.

Which is better for REST API Django or flask?

Flask is able to achieve generally faster performance than Django, generally due to the fact that Flask is much more minimal in design. While both of these frameworks can support several hundred queries per second without breaking a sweat, neither were designed with a priority on speed.


2 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/

like image 122
Sun Liwen Avatar answered Nov 09 '22 23:11

Sun Liwen


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)

like image 39
gsiegman Avatar answered Nov 10 '22 00:11

gsiegman