18👍
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.
9👍
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/
- [Django]-Can someone explain how contribute_to_class works?
- [Django]-Can you check the internet protocol from Django's template?
- [Django]-Represent Ordering in a Relational Database
4👍
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.
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-Django FileField upload is not working for me
- [Django]-Django model method – create_or_update
4👍
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/
- [Django]-Python string to Django timezone (aware datetime)
- [Django]-Is there a built-in login template in Django?
- [Django]-Django – TypeError – save() got an unexpected keyword argument 'force_insert'
2👍
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.
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-Django – [Errno 111] Connection refused