源码分析之ViewSetMixin类

在rest_framework中继承了ModelViewSet类的视图函数,其内部ModelViewSet类继承了一个ViewSetMixin类

ViewSetMixin类重新写了as_view()方法,使得我们可以更加方便快速的写出五个接口!!

image

ViewSetMixin类的位置:

from rest_framework.viewsets import ViewSetMixin

ViewSetMixin类中的as_view()方法分析

# 重写了as_view
# 核心代码(所以路由中只要配置了对应关系,比如{'get':'list'}),当get请求来,就会执行list方法
for method, action in actions.items():
    #method:get
    # action:list
    handler = getattr(self, action)
    #执行完上一句,handler就变成了list的内存地址
    setattr(self, method, handler)
    #执行完上一句  对象.get=list
    #for循环执行完毕 对象.get:对着list   对象.post:对着create
posted @ 2022-10-25 17:05  等日落  阅读(39)  评论(0编辑  收藏  举报