【问题标题】:Throttling Django Rest Framework per user group限制每个用户组的 Django Rest Framework
【发布时间】:2022-11-05 15:46:13
【问题描述】:

这看起来很简单,但却无法实现。我需要为我的应用程序中的每个 API 订阅层设置一个节流阀。我创建了一个 throttling.py 文件,它包含油门类。

视图.py

from jet.throttling import BasicRateThrottle, PlatinumRateThrottle

class JetPullViewSet(viewsets.ViewSet):
     throttle_classes = [BasicRateThrottle, PlatinumRateThrottle]
     permission_classes = [IsAuthenticated]

     def list(self, request):
         user = self.request.user
         queryset = Data.objects.all() 

         serializer = DataSerializer(queryset, many=True, fields=data_var_fields)

         return Response(serializer.data)

节流.py

from rest_framework.throttling import UserRateThrottle

class PlatinumRateThrottle(UserRateThrottle):
    scope = 'platinum'

class BasicRateThrottle(UserRateThrottle):
    scope = 'basic'

设置.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ],

    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.UserRateThrottle',
        'jet.throttling.PlatinumRateThrottle',
        'jet.throttling.BasicRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES': {
        'basic': '5/day',
        'platinum': '200/day',
    }
}

我收到的错误是: “在 /jet/gribpull/ 配置不正确 没有为“基本”范围设置默认油门速率”

尽管它对我来说似乎定义得很清楚。几天来我一直在尝试不同的组合,并引用了Django Throttling Docs 和无数的 SO 帖子。油门默认速率拒绝设置。

非常感谢您的帮助。

【问题讨论】:

    标签: django django-rest-framework throttling drf-queryset


    【解决方案1】:

    我也遇到了同样的问题,之前一切正常。然后为了测试,我注释掉了所有的油门,现在在删除 cmets 之后,代码似乎没有找到我设置的默认油门速率。在这方面需要大量帮助

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2015-05-18
      • 2018-10-19
      • 2013-07-11
      • 2014-07-18
      • 2013-11-15
      • 2013-10-19
      • 2014-07-06
      • 1970-01-01
      相关资源
      最近更新 更多