• drf04 drf视图类


    REST framework 提供了众多的通用视图基类与扩展类,以简化视图的编写。

    1、2个视图基类

    1.1、 APIView

    rest_framework.views.APIView

    APIView是REST framework提供的所有视图的基类,继承自Django的View父类。

    APIViewView的不同之处在于:

    • 传入到视图方法中的是REST framework的Request对象,而不是Django的HttpRequeset对象;

    • 视图方法可以返回REST framework的Response对象,视图会为响应数据设置(render)符合前端要求的格式;

    • 任何APIException异常都会被捕获到,并且处理成合适的响应信息;

    • 在进行dispatch()分发前,会对请求进行身份认证、权限检查、流量控制。

    支持定义的类属性

    • authentication_classes 列表或元祖,身份认证类

    • permissoin_classes 列表或元祖,权限检查类

    • throttle_classes 列表或元祖,流量控制类

    APIView中仍以常规的类视图定义方法来实现get() 、post() 或者其他请求方式的方法。

    举例:

    from rest_framework.views import APIView
    from rest_framework.response import Response

    # url(r'^students/$', views.StudentsAPIView.as_view()),
    class StudentsAPIView(APIView):
       def get(self, request):
           data_list = Student.objects.all()
           serializer = StudentModelSerializer(instance=data_list, many=True)
           return Response(serializer.data)

     

    1.2、 GenericAPIView[通用视图类]

    rest_framework.generics.GenericAPIView

    继承自APIVIew主要增加了操作序列化器和数据库查询的方法,作用是为下面Mixin扩展类的执行提供方法支持。通常在使用时,可搭配一个或多个Mixin扩展类。

    提供的关于序列化器使用的属性与方法

    • 属性:

      • serializer_class 指明视图使用的序列化器

    • 方法:

      • get_serializer_class(self)

        当出现一个视图类中调用多个序列化器时,那么可以通过条件判断在get_serializer_class方法中通过返回不同的序列化器类名就可以让视图方法执行不同的序列化器对象了。

        返回序列化器类,默认返回serializer_class,可以重写,例如:

        def get_serializer_class(self):
           if self.request.user.is_staff:
               return FullAccountSerializer
           return BasicAccountSerializer
      • get_serializer(self, args, *kwargs)

        返回序列化器对象,主要用来提供给Mixin扩展类使用,如果我们在视图中想要获取序列化器对象,也可以直接调用此方法。

        注意,该方法在提供序列化器对象的时候,会向序列化器对象的context属性补充三个数据:request、format、view,这三个数据对象可以在定义序列化器时使用。

        • request 当前视图的请求对象

        • view 当前请求的类视图对象

        • format 当前请求期望返回的数据格式

    提供的关于数据库查询的属性与方法

    • 属性:

      • queryset 指明使用的数据查询集

    • 方法:

      • get_queryset(self)

        返回视图使用的查询集,主要用来提供给Mixin扩展类使用,是列表视图与详情视图获取数据的基础,默认返回queryset属性,可以重写,例如:

        def get_queryset(self):
           user = self.request.user
           return user.accounts.all()
      • get_object(self)

        返回详情视图所需的模型类数据对象,主要用来提供给Mixin扩展类使用。

        在试图中可以调用该方法获取详情信息的模型类对象。

        若详情访问的模型类对象不存在,会返回404。

        该方法会默认使用APIView提供的check_object_permissions方法检查当前对象是否有权限被访问。

        举例:

        # url(r'^books/(?P<pk>d+)/$', views.BookDetailView.as_view()),
        class BookDetailView(GenericAPIView):
           queryset = BookInfo.objects.all()
           serializer_class = BookInfoSerializer

           def get(self, request, pk):
               book = self.get_object() # get_object()方法根据pk参数查找queryset中的数据对象
               serializer = self.get_serializer(book)
               return Response(serializer.data)

    其他可以设置的属性

    • pagination_class 指明分页控制类

    • filter_backends 指明过滤控制后端

    为了方便学习上面的GenericAPIView通用视图类,我们新建一个子应用。

    python manage.py startapp gen
    from rest_framework.generics import GenericAPIView
    
    from students.models import Student
    from .serializers import StudentModelSerializer, StudentModel2Serializer
    from rest_framework.response import Response
    
    class StudentsGenericAPIView(GenericAPIView):
        # 本次视图类中要操作的数据[必填]
        queryset = Student.objects.all()
        # 本次视图类中要调用的默认序列化器[选填]
        serializer_class = StudentModelSerializer
    
        def get(self, request):
            """获取所有学生信息"""
            serializer = self.get_serializer(instance=self.get_queryset(), many=True)
    
            return Response(serializer.data)
    
        def post(self,request):
    
            data = request.data
    
            serializer = self.get_serializer(data=data)
    
            serializer.is_valid(raise_exception=True)
    
            instance = serializer.save()
    
            serializer = self.get_serializer(instance=instance)
    
            return Response(serializer.data)
    
    
    class StudentGenericAPIView(GenericAPIView):
        queryset = Student.objects.all()
    
        serializer_class = StudentModelSerializer
    
        def get_serializer_class(self):
            """重写获取序列化器类的方法"""
            if self.request.method == "GET":
                return StudentModel2Serializer
            else:
                return StudentModelSerializer
    
        # 在使用GenericAPIView视图获取或操作单个数据时,视图方法中的代表主键的参数最好是pk
        def get(self,request,pk):
            """获取一条数据"""
            serializer = self.get_serializer(instance=self.get_object())
    
            return Response(serializer.data)
    
        def put(self,request,pk):
    
            data = request.data
    
            serializer = self.get_serializer(instance=self.get_object(),data=data)
    
            serializer.is_valid(raise_exception=True)
    
            serializer.save()
    
            serializer = self.get_serializer(instance=self.get_object())
    
            return Response(serializer.data)
    gen/views.py
    from rest_framework import serializers
    
    from students.models import Student
    
    class StudentModelSerializer(serializers.ModelSerializer):
        class Meta:
            model= Student
            fields = "__all__"
    
    class StudentModel2Serializer(serializers.ModelSerializer):
        class Meta:
            model= Student
            fields = ("name","class_null")
    gen/serializers.py
    from django.urls import path, re_path
    from . import views
    urlpatterns = [
        path(r"students/",views.StudentsGenericAPIView.as_view()),
        re_path(r"students/(?P<pk>d+)/",views.StudentGenericAPIView.as_view()),
    ]
    gen/urls.py
    from django.contrib import admin
    from django.urls import path,include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path("stu/",include("students.urls"))
    ]
    urls.py
    from django.db import models
    
    # Create your models here.
    class Student(models.Model):
        #以下模型字段默认时不能为空 null=False
        name = models.CharField(max_length=100,verbose_name="姓名")
        sex = models.BooleanField(default=1,verbose_name="性别")
        age = models.IntegerField(verbose_name="年龄")
        class_null = models.CharField(max_length=5,verbose_name="班级编号",null=True)
        description = models.TextField(max_length=1000,verbose_name="个性签名",null=True)
    
        class Meta:
            db_table="tb_student"
            verbose_name ="学生"
            verbose_name_plural = verbose_name
    gen/models.py

    2、 5个视图扩展类

    作用:

    提供了几种后端视图(对数据资源进行曾删改查)处理流程的实现,如果需要编写的视图属于这五种,则视图可以通过继承相应的扩展类来复用代码,减少自己编写的代码量。

    这五个扩展类需要搭配GenericAPIView父类,因为五个扩展类的实现需要调用GenericAPIView提供的序列化器与数据库查询的方法。

     

    1)ListModelMixin

    列表视图扩展类,提供list(request, *args, **kwargs)方法快速实现列表视图,返回200状态码。

    该Mixin的list方法会对数据进行过滤和分页。

    源代码:

    class ListModelMixin(object):
       """
      List a queryset.
      """
       def list(self, request, *args, **kwargs):
           # 过滤
           queryset = self.filter_queryset(self.get_queryset())
           # 分页
           page = self.paginate_queryset(queryset)
           if page is not None:
               serializer = self.get_serializer(page, many=True)
               return self.get_paginated_response(serializer.data)
           # 序列化
           serializer = self.get_serializer(queryset, many=True)
           return Response(serializer.data)

    举例:

    from rest_framework.mixins import ListModelMixin

    class BookListView(ListModelMixin, GenericAPIView):
       queryset = BookInfo.objects.all()
       serializer_class = BookInfoSerializer

       def get(self, request):
           return self.list(request)

     

    2)CreateModelMixin

    创建视图扩展类,提供create(request, *args, **kwargs)方法快速实现创建资源的视图,成功返回201状态码。

    如果序列化器对前端发送的数据验证失败,返回400错误。

    源代码:

    class CreateModelMixin(object):
       """
      Create a model instance.
      """
       def create(self, request, *args, **kwargs):
           # 获取序列化器
           serializer = self.get_serializer(data=request.data)
           # 验证
           serializer.is_valid(raise_exception=True)
           # 保存
           self.perform_create(serializer)
           headers = self.get_success_headers(serializer.data)
           return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

       def perform_create(self, serializer):
           serializer.save()

       def get_success_headers(self, data):
           try:
               return {'Location': str(data[api_settings.URL_FIELD_NAME])}
           except (TypeError, KeyError):
               return {}

     

    3)RetrieveModelMixin

    详情视图扩展类,提供retrieve(request, *args, **kwargs)方法,可以快速实现返回一个存在的数据对象。

    如果存在,返回200, 否则返回404。

    源代码:

    class RetrieveModelMixin(object):
       """
      Retrieve a model instance.
      """
       def retrieve(self, request, *args, **kwargs):
           # 获取对象,会检查对象的权限
           instance = self.get_object()
           # 序列化
           serializer = self.get_serializer(instance)
           return Response(serializer.data)

    举例:

    class BookDetailView(RetrieveModelMixin, GenericAPIView):
       queryset = BookInfo.objects.all()
       serializer_class = BookInfoSerializer

       def get(self, request, pk):
           return self.retrieve(request)

     

    4)UpdateModelMixin

    更新视图扩展类,提供update(request, *args, **kwargs)方法,可以快速实现更新一个存在的数据对象。

    同时也提供partial_update(request, *args, **kwargs)方法,可以实现局部更新。

    成功返回200,序列化器校验数据失败时,返回400错误。

    源代码:

    class UpdateModelMixin(object):
       """
      Update a model instance.
      """
       def update(self, request, *args, **kwargs):
           partial = kwargs.pop('partial', False)
           instance = self.get_object()
           serializer = self.get_serializer(instance, data=request.data, partial=partial)
           serializer.is_valid(raise_exception=True)
           self.perform_update(serializer)

           if getattr(instance, '_prefetched_objects_cache', None):
               # If 'prefetch_related' has been applied to a queryset, we need to
               # forcibly invalidate the prefetch cache on the instance.
               instance._prefetched_objects_cache = {}

           return Response(serializer.data)

       def perform_update(self, serializer):
           serializer.save()

       def partial_update(self, request, *args, **kwargs):
           kwargs['partial'] = True
           return self.update(request, *args, **kwargs)

     

    5)DestroyModelMixin

    删除视图扩展类,提供destroy(request, *args, **kwargs)方法,可以快速实现删除一个存在的数据对象。

    成功返回204,不存在返回404。

    源代码:

    class DestroyModelMixin(object):
       """
      Destroy a model instance.
      """
       def destroy(self, request, *args, **kwargs):
           instance = self.get_object()
           self.perform_destroy(instance)
           return Response(status=status.HTTP_204_NO_CONTENT)

       def perform_destroy(self, instance):
           instance.delete()

     

    使用GenericAPIView和视图扩展类,实现api接口,代码:

    from rest_framework.generics import GenericAPIView
    
    from students.models import Student
    from .serializers import StudentModelSerializer, StudentModel2Serializer
    from rest_framework.response import Response
    
    """GenericAPIView结合视图扩展类实现api接口"""
    from rest_framework.mixins import ListModelMixin,CreateModelMixin
    class Students2GenericAPIView(GenericAPIView,ListModelMixin,CreateModelMixin):
        # 本次视图类中要操作的数据[必填]
        queryset = Student.objects.all()
        # 本次视图类中要调用的默认序列化器[玄天]
        serializer_class = StudentModelSerializer
    
        def get(self, request):
            """获取多个学生信息"""
            return self.list(request)
    
        def post(self,request):
            """添加学生信息"""
            return self.create(request)
    
    
    from rest_framework.mixins import RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
    class Student2GenericAPIView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
        queryset = Student.objects.all()
    
        serializer_class = StudentModelSerializer
    
        # 在使用GenericAPIView视图获取或操作单个数据时,视图方法中的代表主键的参数最好是pk
        def get(self,request,pk):
            """获取一条数据"""
            return self.retrieve(request,pk)
    
        def put(self,request,pk):
            """更新一条数据"""
            return self.update(request,pk)
    
        def delete(self,request,pk):
            """删除一条数据"""
            return self.destroy(request,pk)
    gen/views.py
    其它代码以上面的例子类似!


    3、 GenericAPIView的视图子类

    1)CreateAPIView

    提供 post 方法

    继承自: GenericAPIView、CreateModelMixin

    2)ListAPIView

    提供 get 方法

    继承自:GenericAPIView、ListModelMixin

    3)RetrieveAPIView

    提供 get 方法

    继承自: GenericAPIView、RetrieveModelMixin

    4)DestoryAPIView

    提供 delete 方法

    继承自:GenericAPIView、DestoryModelMixin

    5)UpdateAPIView

    提供 put 和 patch 方法

    继承自:GenericAPIView、UpdateModelMixin

    6)RetrieveUpdateAPIView

    提供 get、put、patch方法

    继承自: GenericAPIView、RetrieveModelMixin、UpdateModelMixin

    7)RetrieveUpdateDestoryAPIView

    提供 get、put、patch、delete方法

    继承自:GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestoryModelMixin

    from rest_framework.generics import GenericAPIView
    
    from students.models import Student
    from .serializers import StudentModelSerializer, StudentModel2Serializer
    from rest_framework.response import Response
    
    """使用GenericAPIView的视图子类进一步简化开发api接口的代码"""
    from rest_framework.generics import ListAPIView,CreateAPIView
    class Students3GenericAPIView(ListAPIView,CreateAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    
    
    from rest_framework.generics import RetrieveAPIView,UpdateAPIView,DestroyAPIView
    from rest_framework.generics import RetrieveUpdateDestroyAPIView # 结合了上面三个子类的功能
    class Student3GenericAPIView(RetrieveUpdateDestroyAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    gen/views.py
    from django.urls import path, re_path
    from . import views
    urlpatterns = [
        # 使用GenericAPIView的视图子类进一步简化开发api接口的代码
        path("students3/", views.Students3GenericAPIView.as_view()),
        re_path("students3/(?P<pk>d+)/", views.Student3GenericAPIView.as_view()),
    
    ]
    gen/urls.py
    其它代码以上面的例子类似!
     

    4、 常用视图集父类(ViewSet、 GenericViewSet、 ModelViewSet、 ReadOnlyModelViewSet

    1) ViewSet

    继承自APIViewViewSetMixin,作用也与APIView基本类似,提供了身份认证、权限校验、流量管理等。

    ViewSet主要通过继承ViewSetMixin来实现在调用as_view()时传入字典(如{'get':'list'})的映射处理工作。

    在ViewSet中,没有提供任何动作action方法,需要我们自己实现action方法。

     

    使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中:

    • list() 提供一组数据

    • retrieve() 提供单个数据

    • create() 创建数据

    • update() 保存数据

    • destory() 删除数据

    ViewSet视图集类不再实现get()、post()等方法,而是实现动作 action 如 list() 、create() 等。

    视图集只在使用as_view()方法的时候,才会将action动作与具体请求方式对应上。如:

    from rest_framework.generics import GenericAPIView
    
    from students.models import Student
    from .serializers import StudentModelSerializer, StudentModel2Serializer
    from rest_framework.response import Response
    
    """使用视图集把上面的两个视图类组成一个视图类"""
    from rest_framework.viewsets import ViewSet
    class StudentViewSet(ViewSet):
        def get_all(self,request):
            """获取所有学生信息"""
            data_list = Student.objects.all()
            serializer = StudentModelSerializer(instance=data_list, many=True)
            return Response(serializer.data)
    
        def add_student(self,request):
    
            return Response({"message":"添加成功!"})
    
        def get_one(self,request,pk):
            """获取一个学生信息"""
            instance = Student.objects.get(pk=pk)
            serializer = StudentModelSerializer(instance=instance)
            return Response(serializer.data)
    
    """发挥下ViewSet的作用"""
    from rest_framework.viewsets import ViewSet
    from rest_framework.generics import ListAPIView,CreateAPIView,RetrieveAPIView
    class Student2ViewSet(ViewSet,ListAPIView,CreateAPIView,RetrieveAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
        def get_all(self,request):
            """获取所有学生信息"""
            return self.list(request)
    
        def add_student(self,request):
    
            return self.create(request)
    
        def get_one(self,request,pk):
            """获取一个学生信息"""
            return self.retrieve(request)
    gen/views.py
    from django.urls import path, re_path
    from . import views
    urlpatterns = [
        # 使用视图集把上面的两个视图类组成一个视图类
        # as_view中提供字典参数,允许我们把http和视图方法进行一对一的映射
        path("students4/",views.StudentViewSet.as_view({"get":"get_all","post":"add_student"})),
        re_path("students4/(?P<pk>d+)/", views.StudentViewSet.as_view({"get":"get_one"})),
    
        # 发挥下ViewSet的作用
        path("students5/", views.Student2ViewSet.as_view({"get": "list", "post": "add_student"})),
        re_path("students5/(?P<pk>d+)/", views.Student2ViewSet.as_view({"get":"get_one"})),
    
    ]
    gen/urls.py 

     

    2)GenericViewSet

    使用ViewSet通常并不方便,因为list、retrieve、create、update、destory等方法都需要自己编写,而这些方法与前面讲过的Mixin扩展类提供的方法同名,所以我们可以通过继承Mixin扩展类来复用这些方法而无需自己编写。但是Mixin扩展类依赖与GenericAPIView,所以还需要继承GenericAPIView

    GenericViewSet就帮助我们完成了这样的继承工作,继承自GenericAPIViewViewSetMixin,在实现了调用as_view()时传入字典(如{'get':'list'})的映射处理工作的同时,还提供了GenericAPIView提供的基础方法,可以直接搭配Mixin扩展类使用。

    举例:

    from rest_framework.viewsets import GenericViewSet
    from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
    class Student4ViewSet(GenericViewSet,ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
       queryset = Student.objects.all()
       serializer_class = StudentModelSerializer

    url的定义

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

    ]

    3)ModelViewSet

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

    4)ReadOnlyModelViewSet

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

     

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

    在视图集中,除了上述默认的方法动作外,还可以添加自定义动作。

    举例:

    from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet
    class StudentModelViewSet(ModelViewSet):
       queryset = Student.objects.all()
       serializer_class = StudentModelSerializer

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

    url的定义

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

       path("stu/login/",views.StudentModelViewSet.as_view({"get":"login"}))

    ]

     

    1.3.3 action属性

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

    例如:

    from rest_framework.viewsets import ModelViewSet
    from students.models import Student
    from .serializers import StudentModelSerializer
    from rest_framework.response import Response
    class StudentModelViewSet(ModelViewSet):
       queryset = Student.objects.all()
       serializer_class = StudentModelSerializer

       def get_new_5(self,request):
           """获取最近添加的5个学生信息"""
           # 操作数据库
           print(self.action) # 获取本次请求的视图方法名
           
           
    通过路由访问到当前方法中.可以看到本次的action就是请求的方法名

     

    from rest_framework.generics import GenericAPIView
    
    from students.models import Student
    from .serializers import StudentModelSerializer, StudentModel2Serializer
    from rest_framework.response import Response
    
    class StudentsGenericAPIView(GenericAPIView):
        # 本次视图类中要操作的数据[必填]
        queryset = Student.objects.all()
        # 本次视图类中要调用的默认序列化器[选填]
        serializer_class = StudentModelSerializer
    
        def get(self, request):
            """获取所有学生信息"""
            serializer = self.get_serializer(instance=self.get_queryset(), many=True)
    
            return Response(serializer.data)
    
        def post(self,request):
    
            data = request.data
    
            serializer = self.get_serializer(data=data)
    
            serializer.is_valid(raise_exception=True)
    
            instance = serializer.save()
    
            serializer = self.get_serializer(instance=instance)
    
            return Response(serializer.data)
    
    
    class StudentGenericAPIView(GenericAPIView):
        queryset = Student.objects.all()
    
        serializer_class = StudentModelSerializer
    
        def get_serializer_class(self):
            """重写获取序列化器类的方法"""
            if self.request.method == "GET":
                return StudentModel2Serializer
            else:
                return StudentModelSerializer
    
        # 在使用GenericAPIView视图获取或操作单个数据时,视图方法中的代表主键的参数最好是pk
        def get(self,request,pk):
            """获取一条数据"""
            serializer = self.get_serializer(instance=self.get_object())
    
            return Response(serializer.data)
    
        def put(self,request,pk):
    
            data = request.data
    
            serializer = self.get_serializer(instance=self.get_object(),data=data)
    
            serializer.is_valid(raise_exception=True)
    
            serializer.save()
    
            serializer = self.get_serializer(instance=self.get_object())
    
            return Response(serializer.data)
    
    
    """GenericAPIView结合视图扩展类实现api接口"""
    from rest_framework.mixins import ListModelMixin,CreateModelMixin
    class Students2GenericAPIView(GenericAPIView,ListModelMixin,CreateModelMixin):
        # 本次视图类中要操作的数据[必填]
        queryset = Student.objects.all()
        # 本次视图类中要调用的默认序列化器[玄天]
        serializer_class = StudentModelSerializer
    
        def get(self, request):
            """获取多个学生信息"""
            return self.list(request)
    
        def post(self,request):
            """添加学生信息"""
            return self.create(request)
    
    
    from rest_framework.mixins import RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
    class Student2GenericAPIView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
        queryset = Student.objects.all()
    
        serializer_class = StudentModelSerializer
    
        # 在使用GenericAPIView视图获取或操作单个数据时,视图方法中的代表主键的参数最好是pk
        def get(self,request,pk):
            """获取一条数据"""
            return self.retrieve(request,pk)
    
        def put(self,request,pk):
            """更新一条数据"""
            return self.update(request,pk)
    
        def delete(self,request,pk):
            """删除一条数据"""
            return self.destroy(request,pk)
    
    
    """使用GenericAPIView的视图子类进一步简化开发api接口的代码"""
    from rest_framework.generics import ListAPIView,CreateAPIView
    class Students3GenericAPIView(ListAPIView,CreateAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    
    
    from rest_framework.generics import RetrieveAPIView,UpdateAPIView,DestroyAPIView
    from rest_framework.generics import RetrieveUpdateDestroyAPIView # 结合了上面三个子类的功能
    class Student3GenericAPIView(RetrieveUpdateDestroyAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    
    
    """使用视图集把上面的两个视图类组成一个视图类"""
    from rest_framework.viewsets import ViewSet
    class StudentViewSet(ViewSet):
        def get_all(self,request):
            """获取所有学生信息"""
            data_list = Student.objects.all()
            serializer = StudentModelSerializer(instance=data_list, many=True)
            return Response(serializer.data)
    
        def add_student(self,request):
    
            return Response({"message":"添加成功!"})
    
        def get_one(self,request,pk):
            """获取一个学生信息"""
            instance = Student.objects.get(pk=pk)
            serializer = StudentModelSerializer(instance=instance)
            return Response(serializer.data)
    
    """发挥下ViewSet的作用"""
    from rest_framework.viewsets import ViewSet
    from rest_framework.generics import ListAPIView,CreateAPIView,RetrieveAPIView
    class Student2ViewSet(ViewSet,ListAPIView,CreateAPIView,RetrieveAPIView):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
        def get_all(self,request):
            """获取所有学生信息"""
            return self.list(request)
    
        def add_student(self,request):
    
            return self.create(request)
    
        def get_one(self,request,pk):
            """获取一个学生信息"""
            return self.retrieve(request)
    
    
    """使用GenericViewSet通用视图集"""
    from rest_framework.viewsets import GenericViewSet
    from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin
    class Student3ViewSet(GenericViewSet,ListModelMixin,CreateModelMixin,RetrieveModelMixin):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
        def get_all(self,request):
            """获取所有学生信息"""
            return self.list(request)
    
        def add_student(self,request):
    
            return self.create(request)
    
        def get_one(self,request,pk):
            """获取一个学生信息"""
            return self.retrieve(request)
    
    """简化上面的代码"""
    from rest_framework.viewsets import GenericViewSet
    from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
    class Student4ViewSet(GenericViewSet,ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    
    
    
    """使用ModelViewSet简写上面的继承代码"""
    from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet
    from rest_framework.decorators import action
    class StudentModelViewSet(ModelViewSet):
        queryset = Student.objects.all()
        serializer_class = StudentModelSerializer
    
        @action(methods=["get","put"],detail=True)
        # methods 设置当前方法允许哪些http请求访问当前视图方法
        # detail 设置当前视图方法是否是操作一个数据
        def login(self,request,pk):
            """学生登录功能"""
            print(self.action)
            print(pk)
            return Response({"message":"登录成功"})
    gen/views.py 全部代码
    from rest_framework import serializers
    
    from students.models import Student
    
    class StudentModelSerializer(serializers.ModelSerializer):
        class Meta:
            model= Student
            fields = "__all__"
    
    
    class StudentModel2Serializer(serializers.ModelSerializer):
        class Meta:
            model= Student
            fields = ("name","class_null")
    gen/serializers.py
    from django.urls import path, re_path
    from . import views
    urlpatterns = [
        path("students/",views.StudentsGenericAPIView.as_view()),
        re_path("students/(?P<pk>d+)/",views.StudentGenericAPIView.as_view()),
    
        # GenericAPIView结合视图扩展类实现api接口
        path("students2/", views.Students2GenericAPIView.as_view()),
        re_path("students2/(?P<pk>d+)/",views.Student2GenericAPIView.as_view()),
    
        # 使用GenericAPIView的视图子类进一步简化开发api接口的代码
        path("students3/", views.Students3GenericAPIView.as_view()),
        re_path("students3/(?P<pk>d+)/", views.Student3GenericAPIView.as_view()),
    
        # 使用视图集把上面的两个视图类组成一个视图类
        # as_view中提供字典参数,允许我们把http和视图方法进行一对一的映射
        path("students4/",views.StudentViewSet.as_view({"get":"get_all","post":"add_student"})),
        re_path("students4/(?P<pk>d+)/", views.StudentViewSet.as_view({"get":"get_one"})),
    
        # 发挥下ViewSet的作用
        path("students5/", views.Student2ViewSet.as_view({"get": "list", "post": "add_student"})),
        re_path("students5/(?P<pk>d+)/", views.Student2ViewSet.as_view({"get":"get_one"})),
    
        # 使用GenericViewSet通用视图集
        path("students6/", views.Student3ViewSet.as_view({"get": "get_all", "post": "add_student"})),
        re_path("students6/(?P<pk>d+)/", views.Student3ViewSet.as_view({"get": "get_one"})),
    
        # 简化上面student6的代码
        path("students7/", views.Student4ViewSet.as_view({"get": "list", "post": "create"})),
        re_path("students7/(?P<pk>d+)/", views.Student4ViewSet.as_view({"get": "retrieve","put":"update","delete":"destroy"})),
    
        path("students8/", views.StudentModelViewSet.as_view({"get": "list", "post": "create"})),
        re_path("students8/(?P<pk>d+)/",
                views.StudentModelViewSet.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),
    
        path("stu/login/",views.StudentModelViewSet.as_view({"get":"login"}))
    
    ]
    
    """使用drf提供路由类router给视图集生成路由列表"""
    # 实例化路由类
    # drf提供一共提供了两个路由类给我们使用,他们用法一致,功能几乎一样
    # DefaultRouter
    #  SimpleRouter
    from rest_framework.routers import DefaultRouter,SimpleRouter
    router = DefaultRouter()
    
    # 注册视图集
    # router.register("路由前缀",视图集类)
    router.register("router_stu",views.StudentModelViewSet)
    
    
    
    # 把生成的路由列表追加到urlpatterns
    print( router.urls )
    urlpatterns += router.urls
    gen/urls.py 全部路由








     

     

  • 相关阅读:
    重新开始学习javase_对象的摧毁
    昨天一日和彭讨论post请求数据的问题
    昨天在公司加班,上午好像就是弄一个ftp的linux服务问题
    昨天有是发现一个新的技术问题
    昨天下午快要下班的时候让他们东软测试
    昨天也没有和家里通话,把时间给了一位同事
    早上8:45到达
    又是一个月初
    今天是下雨天
    从每天开始在工作上才算有点事情
  • 原文地址:https://www.cnblogs.com/linux985/p/11002440.html
Copyright © 2020-2023  润新知