使用 django 相关的 REST API 包(例如sweetpie、piston或django-rest进行非-ORM 调用而不是简单地使用视图)有哪些优缺点?
2 回答
I don't think its really a question of whether the API resources directly map to models or not. Its a matter of these API packages abstracting away much of the boilerplate code that you end up doing like checking the request type, mapping to URLs, and serializing your output. Associating a resource with a model is simply one of many features, allowing you to more easily format the representation of the data.
While I can't really see this as an significant negative, I suppose using an API package does require you to conform to its specs and generally work within the realm of its features. But packages like piston or tastypie are so convenient, I can't really think of a real reason NOT to use them if your goal is to expose a RESTful api. You get so much for free. These packages also tend to include extra authentication functionality, and decorators.
Writing basic django views is just the same as rolling your own API. Either use a package for convenience, or roll your own if you really need something custom that they don't provide.
除了 REST 到 ORM 调用转换之外,一个好的 API 包还可以为您处理许多方面,例如能够支持多个序列化后端、处理身份验证/授权、缓存、节流等。
如果您不希望您的项目将来需要这些好处,那么使用硬编码视图可能是最快的,但情况往往并非如此。