Framework for Implementing REST web service in Django
I'm looking to implement a RESTful interface for a Django application. It is primarily 开发者_运维知识库a data-service application - the interface will be (at this point) read-only.
The question is which Django toolsets / frameworks make the most sense for this task.
I see Django-rest and Django-piston. There's also the option of rolling my own.
The question was asked here, but a good two years back. I'd like to know what the current state of play is.
In this question, circa 2008, the strong majority vote was to not use any framework at all - just create Django views that reply with e.g. JSON. (The question was also addressed, crica 2008, here.)
In the current landscape, what makes the most sense?
NOTE: Since this post was written,
django-piston
is no longer actively maintained. As others have mentioned, look intotastypie
ordjango-rest-framework
.
Indeed, you can roll your own, but there's a lot of boilerplate involved.
django-piston is an exceptionally easy to use, and extensible, micro-framework. In addition to mocking up all the necessary views and url patterns, it supports directly mapping models to a REST interface, which is nice if you have a simple use case. I'd suggest looking into it.
And since this question still rated pretty highly in my searches on Google, I'll add this alternative to the mix: http://django-rest-framework.org/
My initial impression is that it does a very good job of embodying the RESTful API design principles described here: http://readthedocs.org/docs/restful-api-design/en/latest/
Using django-rest-interface
Still true.
It's quite trivial to roll your own. Each REST URI maps to a view function. Each REST method (GET, POST, PUT, DELETE) is a simple condition in the view function.
Done.
One way is to roll your own, or use django-piston which is excellent. But the problem I have with piston is that is kind a made to be attached to a existing django-project to add an API. It is not so much made for building a resource oriented API with support for formats including HTML.
The way I see the use case for Piston is, you have a complete website that serves up html content, but then you would attach an api to that at the url /api/*. Then you go and add Piston to it. With this use case Piston is great, no rewrite needed for existing code and you get whatever you need. It may be that Piston works well without separating the api from the user facing part of the site, but I haven't tried that.
It is easier to try to explain this with some examples:
Bitbucket
- User facing url and content: https://bitbucket.org/jespern
- Api url and content: https://api.bitbucket.org/1.0/users/jespern/
Github
- User facing url and content: https://github.com/jgorset
- Api url and content: https://github.com/jgorset.json
Bitbucket is made with Django and django-piston for the API, while Github is made with Ruby On Rails wich has built in support for resources with different formats. I am not saying that you should switch to RoR, because we all love Django, and I am not saying that you couldn't do this with django, but it would be tedious to do this yourself on every view.
So a co-worker and I decided that we wanted some of the "magic" of RoR in Django, but we didn't want it to be magical, but make our lives easier without abstracting everything. So jgorset wrote Respite which is a little Django framework to make "RESTful" APIs in the same fashion as RoR does, but in Django. It is still in early development, but we use it daily in our work projects and it is highly customizable, much like Django itself. It tries to simplify making resource oriented webpages, and structure your code without getting in your way.
So naturally my recommendation would be to look at, and try Respite: https://github.com/jgorset/django-respite/
tastypie is also an option, I've just tried it and it seemed painless until now. I'm playing with a dummy app that exposes an API to a backbone.js client and I've hit no brick walls with this library. This article got me to try it.
精彩评论