适用于 Django 项目/应用程序的 RESTful API
当您想在 Django 中“RESTify”您的 Django 项目时,您更喜欢什么?
我得出的结论是,确实有三个选项可以做到这一点:
- django-piston http:// /bitbucket.org/jespern/django-piston/wiki/Home
- django-rest-interface http://code.google.com/p/django-rest-interface/
- django-restful-resources http://watchitlater.com/blog/2010/02/django-restful-resources/
对我来说,这样做的正确方法是尝试所有'他们并选择最适合我的一个,所以同时我想听听你的......
谢谢。
What do you prefer when you want to "RESTify" your Django project in Django?
I came to the conclusion that there are really three options to do that:
- django-piston http://bitbucket.org/jespern/django-piston/wiki/Home
- django-rest-interface http://code.google.com/p/django-rest-interface/
- django-restful-resources http://watchitlater.com/blog/2010/02/django-restful-resources/
Right way to do this for me would be to try all of'em and pick the one that is best for me, so meanwhile I'd like to hear yours...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最熟悉 django-piston,所以我自然会引导你朝那个方向发展。
不过,快速浏览一下其他两个,就会发现 django-rest-interface 只是将模型公开为资源,而 django-restful-resources 是某些人的一次性尝试。
如果我没记错的话,Piston 源自 bitbucket.org 自己的网站开发,并且具有很大的灵活性 -您可以从资源的访问方法返回几乎任何对象,而不仅仅是模型实例,并且它将被正确编码。它还内置了对一些不错的功能的支持,例如表单验证(如果你能让它正常工作的话)和请求限制等等。
I'm most familiar with django-piston, so I would naturally steer you in that direction.
A quick glance at the other two, though, indicates that django-rest-interface does nothing more than expose models as resources, and that django-restful-resources is some guy's one-off attempt at the same.
Piston, if I recall correctly, grew out of bitbucket.org's own site development, and allows a lot of flexibility - you can return almost any object from your resource's access methods, not just model instances, and it will be properly encoded. It also has built-in support for some nice features, like form validation (if you can get it to work right, anyway) and request throttling, among other things.
使用 django 1.3 中新的基于类的通用视图,使用自定义序列化器和反序列化器实现您自己的其余接口将非常容易,仅使用库存代码即可复制几乎完整的活塞实现。我用 500 行代码制作了一个基于 View(1.3) 的 Rest 模块,具有通用的 RESTful 资源类和子资源、对关联的自然键支持、json 和 XML 序列化等。该模块确实是根据我的应用程序的要求量身定制的,
我这样做是为了克服活塞代码中的一些限制,例如在处理程序调用 .get() 之前修改查询集(例如使用 .values(...)),或者无法在序列化中使用模型的方法。
如果您根据需要进行操作,几天后您将拥有一组可工作的类和 mixins,您将完全理解并控制它们。
With the new class-based generic views in django 1.3 it will be super-easy to implement your own rest interface, with custom serializers and deserializers, replicating the almost complete piston's implementation using just stock code. I made a View(1.3)-based rest module in 500 lines of code, with generic RESTful resource class and sub resources, natural key support for associations, json and XML serialization and more. the module is really tailored upon my app's requirements
I did it to overcome a couple of limitations in piston's code, like having a query set modified (e.g. With .values(...)) before the handler calling .get() on it, or not being able to use a model's method in serialization.
If you do it as you need it, in a couple of days you'll have a working set of classes and mixins, that you will fully understand and be in control of.
作为编写 django-restful-resources 的“某个人”,我想澄清它存在的原因。它并不是试图将模型公开为资源,而是一种允许将单个 URL 映射到多个不同处理程序方法(每个 HTTP 动词一个)的方法。就这样。它可用于公开模型对象,但也可用于将服务公开为资源或您想要通过单个 URL 和 HTTP 动词进行交互的任何其他内容。如果您正在寻找功能更齐全的解决方案,那么请务必选择 Piston。
As the "some guy" who wrote django-restful-resources I would like to clarify why it exists. It is NOT an attempt to expose models as resources, rather it is a means of allowing a single URL to be mapped to a number of different handler methods, one per HTTP verb. That's all. It can be used to expose model objects, but it can also be used to expose services as resources or anything else that you want to interact with via a single URL and HTTP verbs. If you are looking for a more full-featured solution then by all means go with Piston.
正如 eternicode 所提到的, django-piston 非常出色。它很成熟,功能齐全,并且背后有一个良好的社区。尽管有讨论社区驱动的分叉,但目前它似乎确实缺乏很多持续的开发,因此这种情况可能会改变。
django-tastypie 也非常值得一看,并且似乎有很大的推动力那一刻。
我还刚刚发布了另一个值得考虑的选项:django-rest-framework。它背后有一些非常好的功能,例如 API 自动文档。
正如 saverio 所提到的,它使用 Django 1.3 的基于类的视图,这意味着您可以直接放入它提供的一些 MixIn 类,而无需直接使用框架。 (例如添加 HTTP 内容协商以将输出序列化为多种类型)
As mentioned by eternicode, django-piston is excellent. It's mature, well featured and has a good community behind it. It does seem to be lacking much ongoing development at the moment, although there is talk of a community driven fork, so that may change.
django-tastypie is also well worth a look, and seems to have a lot of impetus behind it at the moment.
I've also just released another option that's worth considering: django-rest-framework. There's a couple of really nice features behind it such as the API auto-documentation.
It uses Django 1.3's class based views, as mentioned by saverio, which means you can just drop in some of the MixIn classes it provides, without having to use the framework outright. (For example adding HTTP content negotiation for serializing output to multiple types)