django-rest-framework – Django REST POST和GET不同的节流范围

django-rest-framework – Django REST POST和GET不同的节流范围,第1张

概述我有 django-rest视图类带有get和post方法的照片,我希望允许用户在一分钟内制作一张POST照片上传一小时和1000张GET照片请求.默认情况下,我可以为所有APIView设置throttle_scope(get和post). 怎么做?使用不同的范围创建两个不同的视图? 谢谢. 解决方案1: 这有点棘手,我没有测试过. 覆盖APIView中的get_throttles方法. clas 我有 django-rest视图类带有get和post方法的照片,我希望允许用户在一分钟内制作一张POST照片上传一小时和1000张GET照片请求.默认情况下,我可以为所有APIVIEw设置throttle_scope(get和post).

怎么做?使用不同的范围创建两个不同的视图?

谢谢.

解决方法 解决方案1:

这有点棘手,我没有测试过.

覆盖APIVIEw中的get_throttles方法.

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

您应该为不同的scope_attr定义自己的ScopedrateThrottle类.

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_throttles和ScopedRateThrottle

总结

以上是内存溢出为你收集整理的django-rest-framework – Django REST POST和GET不同的节流范围全部内容,希望文章能够帮你解决django-rest-framework – Django REST POST和GET不同的节流范围所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193604.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存