gpt4 book ai didi

django-rest-framework - Django REST POST 和 GET 不同的 throttle 范围

转载 作者:行者123 更新时间:2023-12-01 08:53:08 25 4
gpt4 key购买 nike

我有 django-rest View 类 Photo使用 get 和 post 方法,我希望允许用户在一小时内上传一张 POST 照片,并在分钟内进行 1000 次 GET 照片请求。默认我可以设置 throttle_scope所有 APIView (获取和发布)。

如何执行?创建具有不同范围的两个不同 View ?

谢谢。

最佳答案

解决方案1:
这有点棘手,我没有测试它。
覆盖 get_throttles APIView 中的方法。

class PhotoView(APIView):
throttle_scope = 'default_scope'

def get_throttles(self):
if self.request.method.lower() == 'get':
self.throttle_scope = 'get_scope'
elif self.request.method.lower() == 'post':
self.throttle_scope = 'post_scope'

return super(PhotoView, self).get_throttles()
解决方案2
您应该定义自己的 ScopedRateThrottle不同的类 scope_attr .
class FooScopedRateThrottle(ScopedRateThrottle):
scope_attr = 'foo_throttle_scope'

class BarScopedRateThrottle(ScopedRateThrottle):
scope_attr = 'bar_throttle_scope'

class PhotoView(APIView):
foo_throttle_scope = 'scope_get'
bar_throttle_scope = 'scope_post'

def get_throttles(self):
ret = []
if self.request.method.lower() == 'get':
return [FooScopedRateThrottle(), ]
elif self.request.method.lower() == 'post':
return [BarScopedRateThrottle(), ]
else:
return super(PhotoView, self).get_throttles()
供引用。相关源码: get_throttlesScopedRateThrottle

关于django-rest-framework - Django REST POST 和 GET 不同的 throttle 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36039843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com