视图家族

视图家族

**1.View: **

  1. 将请求方式与视图类的同名方法建立映射,完成请求响应

2.APIView

  1. 拥有View的所有功能
  2. 重写as_view禁用csrf认证
  3. 重写dispatch: 请求、响应、渲染、异常、解析、三个认证
  4. 多了一堆类属性,可以完成试图类地局部配置
eg:
	class StudentAPIView(APIView):
        def get(self, request, *args, **kwargs):
        
            stu_query = models.Student.objects.all()
            stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
            
            return Response(stu_ser.data)

3.GenericAPIView:

  1. 拥有APIView的所有功能
  2. 三个方法: get_object()、get_queryset()、get_serializer()
  3. 三个属性: queryset、serializer_class、lookup_url_kwargs
eg:
	class StudentGenericAPIView(GenericAPIView):
        queryset = models.Student.objects.all()
        serializer_class = serializers.StudentModelSerializer
        
        def get(self, request, *args, **kwargs):
        
            # stu_query = models.Student.objects.all()
            stu_query = self.get_queryset()
            
            # stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
            stu_ser = self.get_serializer(stu_query, many=True)
            
            return Response(stu_ser.data)

	

4.mixins包:

  1. 五大工具类: RetrieveModelMinxin , ListModelMixin , CreateModelMixin , UpdateModelMixin,
  2. 六大工具方法:retrieve , list , create, update , partial_update , destroy
from rest_framework import mixins
class StudentMixinGenericAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer
    
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

5.generics包:

  1. 一堆mixins工具类与GenericAPIView视图基类组合
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView

class StudentMixinAPIView(CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer
    # url中单查,不一定必须提供主键,提供一切唯一键的字段名均可
    lookup_url_kwarg = 'id'

    # 有删除需求的接口继承DestroyAPIView,重写destroy完成字段删除
    def destroy(self, request, *args, **kwargs):
        pass

6.ViewSetMixin

  1. 重写as_view(), 完成请求方式与视图方法的自定义映射
  2. 例子: as_view({'get': 'my_get'})
# 视图集
from rest_framework.viewsets import ModelViewSet
class StudentModelViewSet(ModelViewSet):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer

    def my_post(self, request, *args, **kwargs):
        return Response('my post ok')  # 需要在url中做映射

7.视图集基类:

  1. ViewSet(ViewSetMixin, APIView): 可以自定义映射关系的APIView
  2. GeneriViewSet(ViewSetMixin, GenericAPIView): 可以自定义映射关系的GenericAPIView

8.常用Model视图集

  1. ModelViewSet(mixins.CreateModelMixin,
    mixins.RetrieveModelMixin,
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin,
    mixins.ListModelMixin,
    GenericViewSet)
    (某一资源的六大操作视图集)

  2. ReadOnlyModelViewSet(mixins.RetrieveModelMixin,
    mixins.ListModelMixin,
    GenericViewSet)
    (某一资源的只读操作视图集)

posted @   Feeling_afraid  阅读(112)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示