频率组件
-使用:
-第一步,写一个频率类,继承SimpleRateThrottle
#重写get_cache_key,返回self.get_ident(request)
#一定要记住配置一个scop=字符串
class Throttle(SimpleRateThrottle):
scope = 'lxx'
def get_cache_key(self, request, view):
return self.get_ident(request)
-第二步:在setting中配置
# REST_FRAMEWORK = {
#
# 'DEFAULT_THROTTLE_RATES':{
# 'lxx':'3/m'
# }
# }
-局部使用
-在视图类中配置:
-throttle_classes=[Throttle,]
-全局使用
-在setting中配置
'DEFAULT_THROTTLE_CLASSES':['自己定义的频率类'],
-局部禁用
throttle_classes=[]
频率组件分析:
-核心源代码:
def check_throttles(self, request):
for throttle in self.get_throttles():
if not throttle.allow_request(request, self):
self.throttled(request, throttle.wait())
-自定义的频率类:详解代码views.py 25行开始
-SimpleRateThrottle源码