# 转载请留言联系

  • 子类视图:

视图 作用 方法 父类
ListAPIView 查询多条数据 get GenericAPIView
ListModelMixin
CreateAPIView 新增一条数据 post GenericAPIView
CreateModelMixin
RetrieveAPIView 查询一条数据 get GenericAPIView
RetrieveModelMixin
UpdateAPIView 修改一条数据 put,patch GenericAPIView
UpdateModelMixin
DestroyAPIView 删除一条数据 delete GenericAPIView
DestroyModelMixin
RetrieveUpdateAPIView 查询,更新一条数据 get,put,patch GenericAPIView
RetrieveModelMixin UpdateModelMixin
RetrieveUpdateDestroyAPIView 获取,更新,删除一条数据 get,put,patch,delete GenericAPIView
RetrieveModelMixin UpdateModelMixin DestroyModelMixin
  • 对比以及使用(GenericAPIView+五个拓展类 VS 子类视图)

1.GenericAPIView+五个拓展类

class DepartmentListAPIView(ListModelMixin,CreateModelMixin,GenericAPIView):

    queryset = Department.objects.all()
serializer_class = DepartmentSerializer def get(self,request):
"""查询多条数据"""
return self.list(request) def post(self,request):
"""新增一条数据"""
return self.create(request) class DepartmentDetailAPIView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView): queryset = Department.objects.all()
serializer_class = DepartmentSerializer def get(self,request,pk):
"""查询一条数据"""
return self.retrieve(request,pk) def post(self,request,pk):
"""修改部门"""
return self.update(request,pk) def delete(self,request,pk):
"""删除一个部门"""
return self.destroy(request,pk)

2.子类视图

class DepartmentListAPIView(ListAPIView,CreateAPIView):
”“”实现了查询多条数据和新增一条数据的功能"""
queryset = Department.objects.all()
serializer_class = DepartmentSerializer class DepartmentDetailAPIView(RetrieveUpdateDestroyAPIView):
"""实现了查询一条数据、修改一条数据、删除一条数据的功能"""
queryset = Department.objects.all()
serializer_class = DepartmentSerializer

3.子类视图的源码分析

# Concrete view classes that provide method handlers
# by composing the mixin classes with the base view. class CreateAPIView(mixins.CreateModelMixin,
GenericAPIView):
"""
Concrete view for creating a model instance.
"""
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs) class ListAPIView(mixins.ListModelMixin,
GenericAPIView):
"""
Concrete view for listing a queryset.
"""
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs) class RetrieveAPIView(mixins.RetrieveModelMixin,
GenericAPIView):
"""
Concrete view for retrieving a model instance.
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs) class DestroyAPIView(mixins.DestroyModelMixin,
GenericAPIView):
"""
Concrete view for deleting a model instance.
"""
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs) class UpdateAPIView(mixins.UpdateModelMixin,
GenericAPIView):
"""
Concrete view for updating a model instance.
"""
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs) def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs) class ListCreateAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
GenericAPIView):
"""
Concrete view for listing a queryset or creating a model instance.
"""
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs) def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs) class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
GenericAPIView):
"""
Concrete view for retrieving, updating a model instance.
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs) def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs) class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericAPIView):
"""
Concrete view for retrieving or deleting a model instance.
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs) def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs) class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
GenericAPIView):
"""
Concrete view for retrieving, updating or deleting a model instance.
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs) def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs) def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)

实际上就是把(GenericAPIView+五个拓展类)的函数封装一下,return回去而已......

DRF最高封装的子类视图的更多相关文章

  1. DRF框架(五)——context传参,二次封装Response类,两个视图基类(APIView/GenericAPIView),视图扩展类(mixins),子类视图(工具视图),视图集(viewsets),工具视图集

    复习 1.整体修改与局部修改 # 序列化get (给前端传递参数) #查询 ser_obj = ModelSerializer(model_obj) #只传递一个参数,默认是instance的参数,查 ...

  2. drf请求、响应与视图

    目录 一.请求 1 定义 2 常用属性 1).data 2).query_params 二.响应 1 Response 2 构造方法 3 状态码 1)信息告知 - 1xx 2)成功 - 2xx 3)重 ...

  3. drf:restful概念,类继承关系,drf请求封装,drf请求流程,版本控制组件,认证组件(token),权限组件

    1.restful规范 resfful规范的概念最重要: 是一套规范,规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的就是,以前写增删改查的时候需要些四个视图寒素,rest ...

  4. DRF Django REST framework 之 视图组件(四)

    引言 在我们有几十上百的视图类,都有get,post等方法,在功能类似时,会导致大量的重复代码出现,显然还有很多可以优化的地方.这也就有了视图组件,它的功能非常强大,能很好的优化接口逻辑. 视图组件 ...

  5. 二次封装 Reponse,视图家族

    复习 """ 1.整体修改与局部修改 # 序列化 ser_obj = ModelSerializer(model_obj) # 反序列化,save() => cre ...

  6. drf(请求封装/认证/权限/节流)

    1.请求的封装 class HttpRequest(object): def __init__(self): pass @propery def GET(self): pass @propery de ...

  7. 二.drf之使用序列化编写视图

    总结:两功能序列化: a.拿到queryset --->idc = Idc.objects.all() b.将queryset给序列化成类---->serializer = IdcSeri ...

  8. DRF终极封装ViewSet和Router附教程PDF源码

    在DRF官方教程的学习过程中,一个很明显的感受是框架在不断地进行封装,我们自己写框架/工具/脚本/平台也可以模仿模仿,先完成底层代码,再做多层封装,让使用者很容易就上手操作.本文是教程的最后一篇,介绍 ...

  9. DRF的封装:APIView类及五大模块

    目录 一.drf框架的封装特点 1.APIView类 二.drf的基础组件 1.请求模块 1.1 请求模块做了什么 1.2 请求request参数 2.解析模块 3.响应模块 4.渲染模块(了解) 5 ...

随机推荐

  1. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

  2. JavaSE复习(二)集合

    Collection List(存取有序,有索引,可以重复) ArrayList 底层是数组实现的,线程不安全,查找和修改快,增和删比较慢 LinkedList 底层是链表实现的,线程不安全,增和删比 ...

  3. Android之ViewPager 第一课

    想要了解Android新版本的的新特性,从头开始吧,这是Android3.0新加入的widget,以前也接触过,但是没有好好的研究过,今天写了一个小程序,研究一下ViewPager. 这个程序是支持左 ...

  4. vector sort AND 友元

    # include<iostream> # include<string> # include<algorithm> # include<stdio.h> ...

  5. 在es中用scroll查询与completableFuture

    一般而言,es返回数据的上限是10000条,如果超过这个数量,就必须使用scroll查询. 所谓scroll查询就类似DBMS中的游标,或者快照吧,利用查询条件,在第一次查询时,在所有的结果上形成了一 ...

  6. css3 text-fill-color简介

    text-fill-color是什么意思呢?单单从字面上来看就是“文本填充颜色”,不过它实际也是设置对象中文字的填充颜色,和color的效果很相似.如果同时设置text-fill-color和colo ...

  7. idea tomcat 启动报错 org.apache.catalina.core.StandardService.initInternal Failed to initialize connector

    org.apache.catalina.core.StandardService.initInternal Failed to initialize connector org.apache.cata ...

  8. Java中动态代理实现原理深究

    一.前言 笔者平时开发使用“动态代理”不多,最近在看设计模式的时候,“动态代理”又在面前晃了几次,所以这次想从源码的角度去分析动态代理的实现原理,以窥探其精妙~ 二.正文 2.1 静态代理  本文源码 ...

  9. Java进阶

    Java进阶(一)Annotation(注解) Java进阶(二)当我们说线程安全时,到底在说什么 Java进阶(三)多线程开发关键技术 Java进阶(四)线程间通信方式对比 Java进阶(五)NIO ...

  10. BZOJ4423 AMPPZ2013Bytehattan(并查集)

    判断网格图中某两点是否被割开,可以将割边视为边区域视为点,转化为可切割这两点的区域是否连通.于是每次判断使两个区域连通后是否会形成环(边界视为连通),若是则说明被两点被割开.并查集维护. #inclu ...