首页 > 其他分享 >【2022-09-30】DRF从入门到入土(五)

【2022-09-30】DRF从入门到入土(五)

时间:2022-09-30 20:03:34浏览次数:80  
标签:info 09 get self 30 request 2022 import class

DRF视图继承关系表

链接https://www.processon.com/embed/60dec4091e085359888e3e72

2个视图基类

# 之前写的5个接口,我们都是继承APIView
# 还可以继承GenericAPIView:它帮我们干了好多事
	
# GenericAPIView:属性和方法
	-属性
    	-queryset              # 要序列化的数据
    	-serializer_class      # 序列化类
        -----了解------
        -lookup_field          # 通过get_object  获取单个对象的查询key值,value值是路由中传进来的
        -filter_backends       # 过滤类
        -pagination_class      # 分页类
    -方法
    	-get_queryset          # 获取要序列化的数据
        -get_object            # 根据lookup_field配置的参数获取单个对象
        -get_serializer        # 获取序列化类,咱们直接用的
        
        -get_serializer_class  # 获取序列化类,不是咱们直接用的get_serializer调用了它
        -------了解-----
        -filter_queryset       # 跟过滤有关系
        -paginate_xxx          # 跟分页有关

基于APIView写5个接口

urls.py
from django.contrib import admin
from django.urls import path
from app01.views import PublishView, PublishDetailView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('publish/', PublishView.as_view()),
    path('publish/<int:pk>/', PublishDetailView.as_view())
]
models.py
from django.db import models


# Create your models here.

class Publish(models.Model):
    name = models.CharField(max_length=32, verbose_name='出版社名称')
    address = models.CharField(max_length=64, verbose_name='出版社地址')
    contact_way = models.CharField(max_length=32, verbose_name='联系方式')

views.py
from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND


class PublishView(APIView):
    def get(self, request):
        publish_list = Publish.objects.all()
        info = PublishModelSerializer(instance=publish_list, many=True)
        return Response(info.data)

    def post(self, request):
        info = PublishModelSerializer(data=request.data)
        if info.is_valid():
            info.save()
            return Response({'msg': '新增成功'}, status=HTTP_200_OK)
        else:
            return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)


class PublishDetailView(APIView):
    def get(self, request, pk):
        publish = Publish.objects.filter(pk=pk).first()
        info = PublishModelSerializer(instance=publish)
        return Response(info.data)

    def Put(self, request, pk):
        book = Publish.objects.filter(pk=pk).first()
        info = PublishModelSerializer(instance=Publish, data=request.data)
        if info.is_valid():
            info.save()
            return Response({'msg': '修改成功'}, status=HTTP_200_OK)
        else:
            return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)

    def delete(self, request, pk):
        Publish.objects.filter(pk=pk).delete()
        return Response({'msg': '删除成功'}, status=HTTP_200_OK)

Serializer.py
from rest_framework import serializers
from .models import Publish


class PublishModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Publish
        fields = '__all__'

基于GenericAPIView写5个接口

views.py
from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND
from rest_framework.generics import GenericAPIView


class PublishView(GenericAPIView):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

    def get(self, request):
        publish_list = self.get_queryset()
        info = self.get_serializer(instance=publish_list, many=True)
        return Response(info.data)

    def post(self, request):
        info = self.get_serializer(data=request.data)
        if info.is_valid():
            info.save()
            return Response({'msg': '新增成功'}, status=HTTP_200_OK)
        else:
            return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)


class PublishDetailView(GenericAPIView):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

    def get(self, request, pk):
        publish = self.get_object()
        info = self.get_serializer(instance=publish)
        return Response(info.data)

    def Put(self, request, pk):
        book = self.get_object()
        info = self.get_serializer(instance=Publish, data=request.data)
        if info.is_valid():
            info.save()
            return Response({'msg': '修改成功'}, status=HTTP_200_OK)
        else:
            return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)

    def delete(self, request, pk):
        self.get_queryset().filter(pk=pk).delete()
        return Response({'msg': '删除成功'}, status=HTTP_200_OK)

5个视图扩展类

CreateModelMixin:Create方法,就是咱们原来的post                       新增数据
RetrieveModelMixin:retrieve方法,就是咱们原来的get	       	       获取单条数据	
UpdateModelMixin:update方法,就是咱们原来的put		     	       修改数据
ListModelMixin:list方法,就是原来咱们的get			       获取所有数据
DestroyModelMixin:destroy方法,就是原来咱们的delete       	       删除数据


### 必须配合GenericAPIView使用,不能配合APIView使用

基于GenericAPIView+5个视图扩展类编写5个接口

views.py
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin, \
    ListModelMixin


class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

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


class PublishDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def Put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

9个视图子类

# 视图类:继承了GenericAPIView+某个或某几个视图扩展类

基于9个视图子类编写5个接口

views.py
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveAPIView, DestroyAPIView, UpdateAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, \
    RetrieveUpdateAPIView


class PublishView(ListCreateAPIView):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer


class PublishDetailView(RetrieveUpdateDestroyAPIView):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

视图集

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

# list()                   提供一组数据
# retrieve()               提供单个数据
# create()                 创建数据
# update()                 保存数据
# destory()                删除数据
# ViewSet视图集类不再实现get()、post()等方法,而是实现动作 action 如 list() 、create() 等。

继承ModelViewSet编写5个接口

urls.py
from django.contrib import admin
from django.urls import path
from app01.views import PublishView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('publish/', PublishView.as_view({'get': 'list', 'post': 'create'})),
    path('publish/<int:pk>/', PublishView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}))
]

views.py
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet


class PublishView(ModelViewSet):
    queryset = Publish.objects.all()
    serializer_class = PublishModelSerializer

ViewSetMixin源码分析

from rest_framework.viewsets import ViewSetMixin, ViewSet, GenericViewSet

# ViewSet: APIView+ViewSetMixin的组合
# ViewSetMixin: 控制了路由的写法
# GenericViewSet: GenericAPIView+ViewSetMixin的组

"""
ModelViewSet它继承了很多类,其中GenericViewSet继承了GenericAPIView+ViewSetMixin,而ViewSetMixin方法之所以能够控制路由,就是因为它重写了as_view
所以在路由配置中的as_view,其实已经不是APIview中的as_view了,这里的as_view就是ViewSetMixin方法的as_view

继承ViewSetMixin的as_view,是根据继承的查找顺序执行的   userview>>>>ModelViewSet>>>>GenericAPIView>>>>>ViewSetMixin

            mixins.CreateModelMixin,
            mixins.RetrieveModelMixin,
            mixins.UpdateModelMixin,
            mixins.DestroyModelMixin,
            mixins.ListModelMixin,
            GenericViewSet

"""
class ViewSetMixin:
    @classonlymethod
    def as_view(cls, actions=None, **initkwargs):
        # 路由中as_view中必须传参数,必须传字典:{'get': 'list', 'post': 'create'}
        if not actions: 
            raise TypeError("The `actions` argument must be provided when "
                            "calling `.as_view()` on a ViewSet. For example "
                            "`.as_view({'get': 'list'})`")
        # 路由匹配成功,执行view(request),request是老的request
        def view(request, *args, **kwargs):
            # actions={'get': 'list', 'post': 'create'}
            for method, action in actions.items():
                # method:get      action:list
                # self 是视图类的对象中通过反射,查找list,
                # handler视图类中的list方法
                handler = getattr(self, action)
                # 向视图类的对象中,反射 method:get,handler:list方法
                # self.get=list
                setattr(self, method, handler)
            return self.dispatch(request, *args, **kwargs)
        return csrf_exempt(view)


# 只要继承了ViewSetMixin,路由写法就会变,都要写成:views.UserView.as_view({'get': 'list', 'post': 'create'}))
# 这样写好以后,对应的请求方式来了,就会执行配置的方法


# 扩展:
	-以后只要继承了ViewSetMixin,视图类中可以写任意名字的方法,不用非得写get,post,delete

标签:info,09,get,self,30,request,2022,import,class
From: https://www.cnblogs.com/dy12138/p/16745964.html

相关文章

  • Android Studio运行Failed to find Build Tools revision 30.0.3
    问题第一次安装好AndroidStudio2022.5的版本之后开启虚拟机运行文件报错提示FailedtofindBuildToolsrevision30.0.3打开SDK已经安装了33.0.0,报错提示找不到30.......
  • 报告分享|2022年中国数字化办公市场研究报告
     报告链接:http://tecdat.cn/?p=28674数字化办公作为现代化办公的全新模式,其发展可追溯至19世纪末期自泰勒提出了科学式管理理论,进一步的发展归因于相关软硬件技术的创新......
  • 报告分享|2022年企业数字化人才发展白皮书
    报告链接:http://tecdat.cn/?p=28670数字经济时代,企业对数字化人才的需求急剧增长。此报告对数字化人才培养和企业数字化人才发展现状进行梳理和研究,聚焦于金融、零售、能......
  • 报告分享|2022年中国康复医疗行业研究报告
    报告链接:http://tecdat.cn/?p=28683国家卫生健康委联合七个部门于2021年6月份下发《关于加快推进康复医疗工作发展的意见》要求力争到2022年,逐步建立一支数量合理、素质优......
  • 9.30
    今日内容1.字典相关操作2.元组相关操作3.集合相关操作4.字符编码理论5.字符编码实操1.字典相关操作1.类型转换 dict() 字典的转换一般不使用关键字而是自己动手......
  • 9月30日内容总结——数据类型内置方法剩余部分(字典、元组、集合)、字符编码的概念及使
    目录一、字典的内置方法1、类型转换(把其他类型转换成自己的类型)2、取值3、修改数据值4、增加数据值5、删除数据值1.del方法2.pop方法6、统计字典中键值对的个数7、字典三......
  • LeetCode剑指 Offer II 093 最长斐波那契数列
    LeetCode剑指OfferII093最长斐波那契数列classSolution:deflenLongestFibSubseq(self,arr:List[int])->int:n,loc,ans=len(arr),{},0......
  • HTML09.多媒体元素
    多媒体元素video视频audio音频videocontrols:控制控件的显示,取值只能为controls--->controls="controls"某些属性,只有两种属性:不写属性取值为属性名这种属性......
  • idea激活2022.9.30
    2022.9.30亲测有效地址:​​激活地址​​F5TRIB85C7-eyJsaWNlbnNlSWQiOiJGNVRSSUI4NUM3IiwibGljZW5zZWVOYW1lIjoiU2hhbmRvbmcgVW5pdmVyc2l0eSIsImFzc2lnbmVlTmFtZSI6ImFvIGxp......
  • 【2022-09-29】国庆假前
    22:00一个要教育别人的人,最有效的办法是首先教育好自己。                                  ......