DRF自定义调节速率不起作用,默认速率起作用

2024-06-28 10:57:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将所有用户的限制速率设置为每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.节流率

     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)

你知道问题出在哪里吗


Tags: restapidefaultrateframeworkfilterbackendsthrottle