首页 > 其他分享 >drf学习-5

drf学习-5

时间:2022-10-07 11:45:12浏览次数:56  
标签:info get self request 学习 import class drf

drf视图继承关系表

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调用了它

  了解项

        -paginate_xxx    # 跟分页有关 

  -filter_queryset # 跟过滤有关系

基于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加上某个或某几个视图扩展类

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,get,self,request,学习,import,class,drf
From: https://www.cnblogs.com/zzjjpp/p/16759271.html

相关文章

  • 学习笔记-SQL盲注
    盲注在SQL注入过程中,SQL语句执行后,选择的数据不能回显到前端页面,此时需要利用一些方法进行判断或者尝试,这个过程称之为盲注。在盲注中,攻击者根据其返回页面的不......
  • Ruoyi字典源码学习
    此文章属于ruoyi项目实战系列使用目的什么是字典数据:具体的值(0,1,"Y","N"),对应具体的业务逻辑("男","女","是","否")。字典数据不应该只写死在代码中,还应存入数据库,通过管理系......
  • 稀疏&集成的卷积神经网络学习(续)
    昨天跟大家详细的说了分类,定位的一些相关知识,今天把剩下的最后一点知识给大家补充完整,也感谢大家一直的支持,谢谢!昨天的推送告诉大家了分类方案,我们再温习一下:今天我们简单的......
  • 小心深度学习这个“坑”(入门误区详细版)
    定位:深层学习在哪1、深层学习需要什么?数学线性代数:是有关任意维度空间下事物状态和状态变化的规则。概   率:是用来衡量我们对事物在跨时间后不同状态的确信度。编程......
  • drf从入门到精通
    前后端开发模式API接口、接口测试工具postman、restful规范、序列化与反序列化、djangorestframework快速使用rest-framework之APIView基本使用及源码分析、Request类源......
  • STA-静态时序分析学习记录-1
    cell的传播延时是根据电平转换波形上的某些测量点定义的,使用以下四个变量定义这些测量点#输入端口下降沿阙值input_threshold_pct_fall:50.0;#输入端口上升沿阙值in......
  • MYSQY 学习笔记之 练习题
    试题一:取得每个部门最高薪水的人员名单?分析:①先将员工表依据部门编号进行分组并找出每个分组中最高薪水的人员信息②将最高薪水表与原表进行连接#员工信息表mysql>select......
  • 计网学习笔记
    NoteoftheComputerNetworkingReference:DataCommunicationsandNetworking(ForthEdition)ChapterOne网络准则性能性能通常由网络的两个因素决定:吞吐量和......
  • (四)模仿学习-完成后台管理页面查询
    我们从网上下载一份代码学习,模拟如果在实际工作中,我们入职的时候会首先拿到一个不复杂但是技术比较老的项目,因为作为新人很难去直接参与大而且新的项目进行开发。我们从......
  • (五)模仿学习-完成后台管理页面查询
    我们从网上下载一份代码学习,模拟如果在实际工作中,我们入职的时候会首先拿到一个不复杂但是技术比较老的项目,因为作为新人很难去直接参与大而且新的项目进行开发。我们从......