首页 > 其他分享 >drf-08

drf-08

时间:2022-10-10 23:22:24浏览次数:36  
标签:return 08 request self rate num class drf

三大认证-频率

自定义频率类

from rest_framework.throttling import BaseThrottle
import time

class UserThorttle(BaseThrottle):
    VISIT_RECORD = {}
    def __init__(self):
        self.history = None

    def allow_request(self, request, view):
        ip = request.META.get('REMOTE_ADDR')
        nowtime = time.time()
        if ip not in self.VISIT_RECORD:
            self.VISIT_RECORD[ip] = [nowtime, ]
            return True
        self.history = self.VISIT_RECORD.get(ip)
        while self.history and nowtime-self.history[-1] > 60:
            self.history.pop()
        if len(self.history) < 3:
            self.history.insert(0, nowtime)
            return True
        return False

频率源码分析

1.SimpleRateThrottle重写allow_request方法  
2.判断self.rate是否为空--self.rate--时间列表
3.通过反射区获取rate 获取self.history时间列表
4.获取当前时间
5.通过循环判断列表中的最早的时间与当前时间的差额是否大于60,大于删除最早的时间
6.判断列表中的时间个数是否大于self.num_requests=5
7.大于返回False 小于返回True
def allow_request(self, request, view):
    if self.rate is None:
        return True
    self.key = self.get_cache_key(request, view)
    if self.key is None:
        return True
    self.history = self.cache.get(self.key, [])
    self.now = self.timer()
    while self.history and self.history[-1] <= self.now - self.duration:
        self.history.pop()
        if len(self.history) >= self.num_requests:
            return self.throttle_failure()
        return self.throttle_success()
2.实例化时通过反射获取rate--执行get_rate
  获取到rate执行self.parse_rate(self.rate)
  获取返回的值self.num_requests=5, self.duration=60
    def __init__(self):
        if not getattr(self, 'rate', None):
            self.rate = self.get_rate()
        self.num_requests, self.duration = self.parse_rate(self.rate)
3.通过反射获取scope--我们自己写的频率限制
  所以在继承SimpleRateThrottle需要写一个scope类属性 并在配置文件中配置频率限制
    def get_rate(self):
        if not getattr(self, 'scope', None):
            raise ImproperlyConfigured(msg)
        try:
            return self.THROTTLE_RATES[self.scope]
        except KeyError:
            raise ImproperlyConfigured(msg)
4.执行self.parse_rate(self.rate)
  将{'user_throttle': '5/m'}
  将字符串'5/m'通过/分割并通过解压赋值num='5' period='m'
  将字符串num='5'转为整数5给到num_requests=5
  通过索引拿到period的第一个字符串并通过字典取值获取对应的时间duration=60
  返回num_requests=5,duration=60
    def parse_rate(self, rate):
        if rate is None:
            return (None, None)
        num, period = rate.split('/')
        num_requests = int(num)
        duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        return (num_requests, duration)

分页

1.必须是继承GenericAPIView+ListModelMixin的子视图类上

1.方式一
from rest_framework.pagination import CursorPagination
class BookPage(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 2
    ordering = 'price'
2.方式二
from rest_framework.pagination import PageNumberPagination
class BookPage(PageNumberPagination):
    page_size = 2
    page_query_param = 'page'
    page_size_query_param = 5
    max_page_size = 5
3.方式三
from rest_framework.pagination import LimitOffsetPagination
class BookPage(LimitOffsetPagination):
    default_limit = 2
    limit_query_param = 'limit'
    offset_query_param = 'offset'
    max_limit = 5
 视图类中导入
class BookView(ViewSetMixin, ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    pagination_class = BookPage
注:1.写一个分页类 
    2.继承CursorPagination、PageNumberPagination、LimitOffsetPagination中的一个
    3.写类属性
      CursorPagination:

排序

1.必须是继承GenericAPIView+ListModelMixin的子视图类上
2.路径:http://127.0.0.1:8000/books/?ordering=price

1.from rest_framework.filters import OrderingFilter
class BookView(ViewSetMixin, ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    filter_backends = [OrderingFilter, ]
    ordering_fields = ['price', ]
注:1.继承OrderingFilter排序类
   2.局部导入:filter_backends = [OrderingFilter, ]
   3.排序字段:ordering_fields = ['price', ]

过滤

1.必须是继承GenericAPIView+ListModelMixin的子视图类上
2.路径:http://127.0.0.1:8000/books/?search=救赎

1.from rest_framework.filters import SearchFilter
class BookView(ViewSetMixin, ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    filter_backends = [SearchFilter, ]
    search_fields = ['name', 'publish']
 注:继承过滤类SearchFilter
    局部导入:filter_backends = [SearchFilter, ]
    过滤字段:search_fields = ['name', 'publish'] 如果是多个字段之间是或的关系

作业

1 带排序,带按名字过滤

1.视图类:
from rest_framework.filters import OrderingFilter, SearchFilter

class BookView(ViewSetMixin, ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    filter_backends = [OrderingFilter, SearchFilter]
    ordering_fields = ['price', ]
    search_fields = ['name', ]
2.网址:
http://127.0.0.1:8000/books/?ordering=-price&search=云边 先按价格降序 然后按名字过滤

image

2 继承APIView,实现分页,返回格式跟之前一样

class BookView1(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        page = BookPage()
        res = page.paginate_queryset(book_list, request)
        book_obj = BookSerializer(instance=res, many=True)
        count = len(book_list)
        url = request.path
        path = request.META.get('REMOTE_ADDR')+url
        previous = request.query_params.get('offset')
        return Response({'count': count, 'next': path, 'previous': previous, 'results': book_obj.data})

标签:return,08,request,self,rate,num,class,drf
From: https://www.cnblogs.com/040714zq/p/16777816.html

相关文章

  • drf分页、排序、过滤
    drf分页、排序、过滤自定义频率类#首先我们导入时间模块用来计时importtime#创建一个类继承BaseThrottleclassFrequency(BaseThrottle):#创建一个字典用来......
  • 【2022-10-10】DRF从入门到入土(八)
    drf组件之自定义频率使用fromrest_framework.throttlingimportBaseThrottle,SimpleRateThrottleclassMyThrottle(BaseThrottle):access_record={}de......
  • drf之频率类,分页,排序,过滤
    一、自定义频率类#我们之前写的频率类其实是可以继承两个的SimpleRateThrottle,BaseThrottle#只不过现继承BaseThrottle需要重写BaseThrottle方法我们现在按照继......
  • 【Django-rest-framework框架】 第08回 自定义频率类,分页功能,排序功能,过滤功能
    目录1.自定义频率类2.频率功能源码剖析3.分页功能3.1PageNumberPagination3.2LimitOffsetPagination3.3CursorPagination3.4drf中分页的使用4.排序功能5.过滤功能......
  • 【2022.10.10】drf(8)
    今日学习内容1.自定义评率类2.频率功能源码剖析3.分页功能4.排序功能5.过滤功能5.1内置过滤1自定义频率类fromrest_framework.throttlingimportBaseThr......
  • drf-频率类及分页排过滤序
    1自定义频率类取出访问者ip判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问,在字典里,继续往下走循环判断当前ip的列表,有值,并且当前时间减去列表的最......
  • 玩转树莓派[08安装x11vnc并设置开机自动启动]
    title:玩转树莓派[08:安装x11vnc并设置开机自动启动]excerpt:没钱买显示器,论有界面的重要性~tags:[raspberry,基地2.0,系统,uos,x11vnc]categories:[学习,ra......
  • DRF
    DRF频率类#某个接口,限制访问频率----》可以根据IP,用户id#频率类的编写-第一步:写一个类,继承SimpleRateThrottle-第二步:重写get_cache_key方法-第三步:返......
  • drf 之分页,过滤,排序
    自定义频率类classMyThrottle(SimpleRateThrottle):VISIT_RECORD={}#存放用户访问记录{ip1:[时间1,时间2].。。}def__init__(self):self.hi......
  • Day08BOM
    概述:BOM(bowserobjectmodel)用于操作浏览器的相关内容。BOM缺乏规范,所以通过产生一系列的共用对象来解决这个问题。尽管这些共用的对象也存在对应的兼容问题,w3c介入后兼容......