视图家族

视图家族

**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 @ 2019-11-28 21:35  Feeling_afraid  阅读(112)  评论(0编辑  收藏  举报