目录
Django DRF 限流
1. 使用
1.1 设置认证全局变量
在settings.py添加如下代码
REST_FRAMEWORK = {
"DEFAULT_THROTTLE_CLASSES": ['utils.throttle.MineThrottle']
}
1.2 为单个视图方法设置权限
class Home2(APIView):
throttle_classes = [MineThrottle]
permission_classes = [MinePermission, MinePermission1, MinePermission2]
def check_permissions(self, request):
permission_result = []
for permission in self.get_permissions():
permission_result.append(permission.has_permission(request, self))
if True not in permission_result:
self.permission_denied(
request,
message=getattr(permission, 'message', None),
code=getattr(permission, 'code', None)
)
def get(self, reqeust):
return JsonResponse({"status": True, 'data': 'OK'})
def post(self, request):
print(request.user, request.auth)
return JsonResponse({"status": True, 'data': 'OK'})