django rest框架中的generics vs viewset,如何更喜欢使用哪个? [英] generics vs viewset in django rest framework, how to prefer which one to use?

查看:77
本文介绍了django rest框架中的generics vs viewset,如何更喜欢使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更喜欢使用泛型和视图集之一?换句话说,
应该何时使用泛型,何时应该使用viewset来构建api。
我知道它做同样的事情,但是viewset有路由器,所以在什么情况下泛型要比viewset好?

How to prefer which one of generics and viewset to use? in other words when should I use generics and when should i use viewset for building api. I know that it does the same thing, but viewset has routers, so in what situation generics is better then viewset?

推荐答案

让我们看看它们是不同的。

They are different, let's see.

DRF有两个主要的视图处理系统:

DRF has two main systems for handling views:


  1. APIView :这为 http动词 get post put 补丁删除

  2. ViewSet :这是对APIView的抽象,它提供了动作作为方法:


    • list :只读,返回多个资源(http动词: get )。返回字典列表。

    • 检索:只读,单个资源(http动词: get ,但网址中应包含id)。返回单个字典。

    • create :创建一个新资源(http动词: post

    • update / partial_update :编辑资源(http动词: put / patch

    • 销毁:删除资源(http动词:删除

  1. APIView: This provides methods handler for http verbs: get, post, put, patch, and delete.
  2. ViewSet: This is an abstraction over APIView, which provides actions as methods:
    • list: read only, returns multiple resources (http verb: get). Returns a list of dicts.
    • retrieve: read only, single resource (http verb: get, but will expect an id in the url). Returns a single dict.
    • create: creates a new resource (http verb: post)
    • update/partial_update: edits a resource (http verbs: put/patch)
    • destroy: removes a resource (http verb: delete)

两者都可以与常规Django URL一起使用。

Both can be used with normal django urls.

由于通过操作建立的约定, ViewSet 也具有映射到路由器,这确实很有帮助。

Because of the conventions established with the actions, the ViewSet has also the ability to be mapped into a router, which is really helpful.

现在,这两个视图都具有快捷方式,这些快捷方式为您提供了易于使用的简单实现。

Now, both of this Views, have shortcuts, these shortcuts give you a simple implementation ready to be used.

GenericAPIView :对于 APIView ,这为您提供了与您的数据库模型紧密对应的快捷方式。为标准列表视图和详细信息视图添加通常需要的行为。给您一些属性,例如 serializer_class ,还提供 pagination_class filter_backend

GenericAPIView: for APIView, this gives you shortcuts that map closely to your database models. Adds commonly required behavior for standard list and detail views. Gives you some attributes like, the serializer_class, also gives pagination_class, filter_backend, etc

GenericViewSet :有许多GenericViewSet,最常见的是 ModelViewSet 。它们从 GenericAPIView 继承,并具有所有动作的完整实现:​​ list 检索销毁已更新等。当然,您还可以选择其中的一些内容,阅读文档

GenericViewSet: There are many GenericViewSet, the most common being ModelViewSet. They inherit from GenericAPIView and have a full implementation of all of the actions: list, retrieve, destroy, updated, etc. Of course, you can also pick some of them, read the docs.

因此,回答您的问题:,如果您执行的操作非常简单,只需使用 ModelViewSet 即可,甚至重新定义并调用 super 也足够。对于更复杂的情况,您可以参加较低级别的课程。

So, to answer your question: DRY, if you are doing something really simple, with a ModelViewSet should be enough, even redefining and calling super also is enough. For more complex cases, you can go for lower level classes.

希望对您有所帮助!

这篇关于django rest框架中的generics vs viewset,如何更喜欢使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆