简体   繁体   English

Django 和 Restful API

[英]Django and Restful APIs

I have been struggling with choosing a methodology for creating a RESTful API with Django.我一直在努力选择一种方法来创建带有 Django 的 RESTful API。 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.来自http://fi.am的 WAPI 可能是最接近我想要完成的,但是我不确定在真正的 RESTful API 中是否可以接受将资源标识符的参数放在查询字符串中而不是在“干净” URL 格式。 Any suggestions for modifying WAPIs RestBinding.PATTERN to "clean" up the URLs?有关修改 WAPI RestBinding.PATTERN 以“清理” URL 的任何建议? Another option I've explored is Django-Rest-Interface.我探索的另一个选择是 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).然而,这个框架似乎违反了我需要的最重要的部分之一,那就是包含完整的资源 URL 以引用其他资源(参见http://jacobian.org/writing/rest-worst-practices/Improper Use of链接)。 The final option is to use django-multiresponse and basically do it the long way.最后的选择是使用 django-multiresponse 并且基本上可以做到这一点。

Please offer me your best advice, especially people that have dealt with this decision.请给我你最好的建议,尤其是那些处理过这个决定的人。

For Django, besides tastypie and piston, django-rest-framework is a promising one worth mentioning.对于Django来说,除了美味的派和活塞,django-rest-framework是值得一提的有前途的。 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. 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.以官方网站为例,以上所有代码都提供了 api、自我解释文档(如基于 soap 的 Web 服务)甚至沙盒测试。 Very convenient.很方便。

Links: http://django-rest-framework.org/链接: http://django-rest-framework.org/

I believe the recently released django-piston is now the best solution for creating a proper REST interface in Django.我相信最近发布的 django-piston 现在是在 Django 中创建适当的 REST 接口的最佳解决方案。 django-piston django-活塞

Note : django-piston seems to no longer be maintained (see comments below)注意:django-piston 似乎不再被维护(见下面的评论)

django-tastypie is a good way to do it, their slogan: "Creating delicious APIs for Django apps since 2010" is pretty comforting;) django-tastypie是一个很好的方法,他们的口号是:“自 2010 年以来为 Django 应用程序创建美味的 API”非常令人欣慰;)

You could take look at django-dynamicresponse , which is a lightweight framework for adding REST API with JSON to your Django applications. 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.只需极少的更改即可将 API 支持添加到现有的 Django 应用程序中,并且可以直接在新项目中从一开始就内置 API。

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.基本上,除了将返回的上下文序列化为 JSON 或根据请求类型有条件地呈现模板/重定向之外,它还包括将 JSON 解析为 request.POST 的中间件支持。

This approach differs from other frameworks (such as django-piston) in that you do not need to create separate handlers for API requests.这种方法与其他框架(例如 django-piston)的不同之处在于您不需要为 API 请求创建单独的处理程序。 You can also reuse your existing view logic, and keep using form validation etc. like normal views.您还可以重用现有的视图逻辑,并像普通视图一样继续使用表单验证等。

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/ .查看 django-apibuilder,可从http://opensource.washingtontimes.com/projects/django-apibuilder/ 获得 Perhaps it can be useful?也许它会有用?

/Jesper /杰斯珀

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

Savory Pie is a REST framework that supports django. Savory Pie 是一个支持 django 的 REST 框架。

I would suggest you look into Django Rest Framework (DRF), play around with this and see if it suits your requirements.我建议您查看 Django Rest 框架(DRF),尝试一下,看看它是否符合您的要求。 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.我推荐 DRF 的原因是因为它使 API 视图变得非常简单,使用 GenericAPIView 类、Mixin 类和在通用视图中混合。 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.您可以轻松地使用久经考验的设计模式来制作 API 端点,并保持代码库简洁明了。 You also DRY when writing your code which is always great.您在编写代码时也很干燥,这总是很棒。 Your API views are literally 2-3 lines long.您的 API 视图实际上是 2-3 行长。

You can checkout this tutorialhttp://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. You can checkout this tutorialhttp://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.免责声明:我是该网站的创建者。

Have a look at this RestifyDjango .看看这个RestifyDjango

Somewhat related are Django XML-RPC and JSON-RPC .有些相关的是 Django XML-RPCJSON-RPC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM