【问题标题】:DRF Custom throttle rate doesn't work, default rate worksDRF 自定义油门速率不起作用,默认速率起作用
【发布时间】:2020-09-10 00:54:07
【问题描述】:

我正在尝试将所有用户的限制速率设置为每 15 分钟 100 个请求。

问题是当我覆盖 AnonRateThrottleUserRateThrottle 时,节流根本不起作用。

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'DEFAULT_THROTTLE_CLASSES': [
         'rest_framework.throttling.AnonRateThrottle',
         'rest_framework.throttling.UserRateThrottle'
     ],
     'DEFAULT_THROTTLE_RATES': { # I've lowered the rates to test it
         'anon': '2/min',
         'user': '2/min'
     }

}

完美运行。

这不起作用:

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'DEFAULT_THROTTLE_CLASSES': [
        'api.throttle_rates.AnonHundredPerFifteenMinutesThrottle',
        'api.throttle_rates.UserHundredPerFifteenMinutesThrottle',
    ],
}

   

api.throttle_rates

     from rest_framework.throttling import AnonRateThrottle, UserRateThrottle

    class AnonHundredPerFifteenMinutesThrottle(AnonRateThrottle):
        def parse_rate(self, rate):
            return (2, 60)
    
    
    class UserHundredPerFifteenMinutesThrottle(UserRateThrottle):
        def parse_rate(self, rate):
            return (2,60)

你知道问题出在哪里吗?

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    如果你查看allow_request 函数

    self.rate 始终为None,因为您没有设置它, 因此请求是允许的

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2019-08-16
      • 1970-01-01
      相关资源
      最近更新 更多