django-rest-framework:api版本控制 [英] django-rest-framework: api versioning
问题描述
所以在这里搜索一下似乎普遍的共识是在REST URI中嵌入版本号是一个不好的做法,这是一个坏主意。
so googling around it appears that the general consensus is that embedding version numbers in REST URIs is a bad practice and a bad idea.
支持这个的支持者。
eg API版本控制的最佳做法
我的问题是关于如何在django-rest-framework中完成使用accept header / content negotiation的解决方案。
My question is about how to accomplish the proposed solution of using the accept header / content negotiation in the django-rest-framework to accomplish this.
看起来像框架内的内容谈判,
http:// django-rest-framework.org/api-guide/content-negotiation/
已配置为根据接受的MIME类型自动返回预期值。如果我开始使用Accept标头进行自定义类型,我将失去该框架的好处。
It looks like content negotiation in the framework,
http://django-rest-framework.org/api-guide/content-negotiation/
is already configured to automatically return intended values based on accepted MIME types. If I start using the Accept header for custom types, I'll lose this benefit of the framework.
在框架中是否有更好的方法来实现?
Is there a better way to accomplish this in the framework?
推荐答案
p>这样做的一个方法是将版本控制指定为媒体类型的一部分。
One way of doing this is to have the versioning specified as part of the media type.
这是GitHub 目前为他们的API 。
您还可以在您的接受标头中包含媒体类型参数,例如 Accept:application / json; version = beta
,这将成功匹配 JSONRenderer
。然后,您可以根据接受的媒体类型对视图进行编码,具体行为有所不同,请参阅这里。
You can also include media type parameters in your accept headers, eg Accept: application/json; version=beta
, which will successfully match against JSONRenderer
. You can then code your view to behave differently depending on the accepted media type, see here.
API中有很多不同的版本控制模式,我不会说合适的方法有很大的共识,但这是一个合理的可能性。
There's lots of different patterns for versioning in APIs, and I wouldn't say there's any great consensus around the right approach yet, but that'd be one reasonable possibility.
更新2015年1月:更好的版本控制支持将在3.1.0版本中输入。请参阅[此拉动请求]
Update Jan 2015: Better versioning support will be incoming in the 3.1.0 release. See [this pull request]
2015年3月更新:版本API的文档现在可用。
Update March 2015: Docs for the versioning API are now available.
(https://github.com/tomchristie/django-rest-framework/pull/2285 )了解更多详情。
这篇关于django-rest-framework:api版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!