Django REST framework 源码剖析-视图集详解(ViewSet)

Django REST framework视图图解

  • 视图类(View)

‌视图‌是DRF中处理用户请求的基本单元。它们可以是函数视图(FBV)或类视图(CBV)。函数视图使用函数来处理请求,而类视图则使用类来处理请求。类视图默认会自动dispatch特定的HTTP方法,如GET、POST等,不需要像函数视图那样通过单独的条件判断来处理不同的HTTP方法。此外,类视图可以使用面向对象的技术,如Mixin(多继承、混用),将代码拆解成可复用的组件‌,GenericAPIView可以与ListModelMixin、CreateModelMixin、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin等Mixin结合使用,以实现增删改查等功能‌。

  • 视图集(ViewSet)

‌视图集‌是DRF中用于组织相关视图的集合。它们继承自APIView或GenericAPIView,并且可以定义多个方法(如list、create、retrieve、update、destroy等),这些方法对应不同的HTTP请求。视图集可以解决路由匹配和合并的问题,并且方法名可以自定义,路由匹配规则也需要相应修改。视图集还可以与Mixin结合使用,以实现更复杂的业务逻辑‌。

视图集 ViewSet

  • 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中
  • ViewSet视图集类不再实现get()、post()等方法,而是实现动作 action 如 list() 、create() 等
  • 视图集只在使用as_view()方法的时候,才会将action动作与具体请求方式对应上

动作 action

  • list() 提供一组数据
  • retrieve() 提供单个数据(详情)
  • create() 创建数据
  • update() 保存数据
  • destory() 删除数据

常用视图集父类 ViewSet

  • 继承自APIView与ViewSetMixin,作用也与APIView基本类似,提供了身份认证、权限校验、流量管理等
  • ViewSet主要通过继承ViewSetMixin来实现在调用as_view()时传入字典(如{'get':'list'})的映射处理工作
  • 在ViewSet中,没有提供任何动作action方法,需要我们自己实现action方法

视图示例

python 复制代码
from rest_framework.response import Response
from rest_framework import viewsets
from myproject.models import BookInfo
from myproject.serializer import BookInfoSerializer


class BookInfoViewSet(viewsets.ViewSet):

    def list(self, request):
        books = BookInfo.objects.all()
        serializer = BookInfoSerializer(books, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None):
        try:
            books = BookInfo.objects.get(id=pk)
        except BookInfo.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = BookInfoSerializer(books)
        return Response(serializer.data)

路由示例

python 复制代码
from django.urls import path, re_path
from myproject.viewsets import BookInfoViewSet


urlpatterns = [
    url(r'^books/$', BookInfoViewSet.as_view({'get':'list'}),
    url(r'^books/(?P<pk>\d+)/$', BookInfoViewSet.as_view({'get': 'retrieve'})
]

常用视图集父类 GenericViewSet

  • 使用ViewSet通常并不方便,因为list、retrieve、create、update、destory等方法都需要自己编写,而这些方法与前面讲过的Mixin扩展类提供的方法同名,所以我们可以通过继承Mixin扩展类来复用这些方法而无需自己编写,但是Mixin扩展类依赖与GenericAPIView,所以还需要继承GenericAPIView
  • GenericViewSet就帮助我们完成了这样的继承工作,继承自GenericAPIView与ViewSetMixin,在实现了调用as_view()时传入字典(如{'get':'list'})的映射处理工作的同时,还提供了GenericAPIView提供的基础方法,可以直接搭配Mixin扩展类使用

视图示例

python 复制代码
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin


class EmployeeViewSet(GenericViewSet,ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    queryset = Student.objects.all()
    serializer_class = StudentModelSerializer

路由示例

python 复制代码
from django.urls import path, re_path
from myproject import views


urlpatterns = [
    path("employee/", views.EmployeeViewSet.as_view({"get": "list", "post": "create"})),
    re_path("employee/(?P<pk>\d+)/", views.EmployeeViewSet.as_view({"get": "retrieve","put":"update","delete":"destroy"})),
]

常用视图集父类 ModelViewSet

  • 继承自GenericViewSet,同时包括了ListModelMixin、RetrieveModelMixin、CreateModelMixin、UpdateModelMixin、DestoryModelMixin

视图示例

python 复制代码
from rest_framework.viewsets import ModelViewSet
from myproject.models import Book
from myproject.serializer import BookSerializer


class CustomModelViewSet(ModelViewSet):
    """
    list:获取列表数据
    retrieve:获取详情数据
    crate:新增数据
    update:更新信息
    DELETE:删除
    """
    queryset = Book.objects.all()
    serializer_class = BookSerilizer

路由示例

python 复制代码
from django.urls import path, re_path
from myproject import views


urlpatterns = [
    path("employee_model/", views.CustomModelViewSet.as_view({"get": "list", "post": "create"})),
    re_path("employee_model/(?P<pk>\d+)/", views.CustomModelViewSet.as_view({"get": "retrieve","put":"update","delete":"destroy"})),
]

常用视图集父类 ReadOnlyModelViewSet

  • 继承自GenericViewSet,同时包括了ListModelMixin、RetrieveModelMixin

视图示例

python 复制代码
from rest_framework.viewsets import ReadOnlyModelViewSet
from myproject.models import Book
from myproject.serializer import BookSerilizer


class CustomReadOnlyModelViewSet(ReadOnlyModelViewSet):
    """
    list:获取列表数据
    retrieve:获取详情数据
    """
    queryset = Book.objects.all()
    serializer_class = BookSerilizer

路由示例

python 复制代码
from django.urls import path, re_path
from myproject import views


urlpatterns = [
    path("employee_readonly/", views.CustomReadOnlyModelViewSet.as_view({"get": "list", "post": "create"})),
    re_path("employee_readonly/(?P<pk>\d+)/", views.CustomReadOnlyModelViewSet.as_view({"get": "retrieve","put":"update","delete":"destroy"})),
]

视图集中自定义附加action动作

视图示例

python 复制代码
from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet
from rest_framework.response import Response
from myproject.models import Employee
from myproject.serializer import EmployeeModelSerializer


class EmployeeModelViewSet(ModelViewSet):
    queryset = Employee.objects.all()
    serializer_class = EmployeeModelSerializer

    def login(self,request):
        """登录功能"""
        return Response({"message":"登录成功"})

路由示例

python 复制代码
from django.urls import path, re_path
from myproject import views


urlpatterns = [
    path("employee_action/", views.StudentModelViewSet.as_view({"get": "list", "post": "create"})),
    re_path("employee_action/(?P<pk>\d+)/", views.StudentModelViewSet.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),
    path("employee_action/login/",views.StudentModelViewSet.as_view({"get":"login"}))
]

视图集中action属性

  • 在视图集中,我们可以通过action对象属性来获取当前请求视图集时的action动作是哪个

视图示例

python 复制代码
from rest_framework.viewsets import ModelViewSet
from employee.models import Employee
from .serializers import EmployeeModelSerializer
from rest_framework.response import Response


class StudentModelViewSet(ModelViewSet):
    queryset = Employee.objects.all()
    serializer_class = EmployeeModelSerializer

    def get_top_5(self,request):
        """获取前5条信息"""
        # 获取本次请求的视图方法名
        print(self.action) 

路由示例

python 复制代码
from django.urls import path, re_path
from myproject import views


urlpatterns = [
	path("employee/get_top5/",views.StudentModelViewSet.as_view({"get":"get_top_5"}))
]

总结

两个基类

  • ViewSet: 继承ViewSetMixin和APIView
  • GenericViewSet: 继承ViewSetMixin, generics.GenericAPIView
  • ViewSetMixin: 重写了as_view

两个拓展类

  • ModelViewSet: 继承mixins.CreateModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,mixins.ListModelMixin,GenericViewSet
  • ReadOnlyModelViewSet:继承mixins.RetrieveModelMixin,mixins.ListModelMixin,GenericViewSet
相关推荐
belldeep17 小时前
python:pyTorch 入门教程
pytorch·python·ai·torch
YJlio17 小时前
Registry Usage (RU) 学习笔记(15.5):注册表内存占用体检与 Hive 体量分析
服务器·windows·笔记·python·学习·tcp/ip·django
奔波霸的伶俐虫17 小时前
redisTemplate.opsForList()里面方法怎么用
java·开发语言·数据库·python·sql
longze_717 小时前
生成式UI与未来AI交互变革
人工智能·python·ai·ai编程·cursor·蓝湖
weixin_4380774917 小时前
CS336 Assignment 4 (data): Filtering Language Modeling Data 翻译和实现
人工智能·python·语言模型·自然语言处理
小郭团队17 小时前
未来PLC会消失吗?会被嵌入式系统取代吗?
c语言·人工智能·python·嵌入式硬件·架构
yesyesido17 小时前
智能文件格式转换器:文本/Excel与CSV无缝互转的在线工具
开发语言·python·excel
王夏奇17 小时前
python在汽车电子行业中的应用1-基础知识概念
开发语言·python·汽车
子夜江寒17 小时前
基于PyTorch的CBOW模型实现与词向量生成
pytorch·python
He_Donglin17 小时前
Python图书爬虫
开发语言·爬虫·python