Milano
Milano

Reputation: 18735

DRF Custom throttle rate doesn't work, default rate works

I'm trying to set up the throttle rate for all users to 100 requests per 15 minutes.

The problem is that when I override AnonRateThrottle and UserRateThrottle, the throttling doesn't work at all.

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'
     }

}

Works perfectly.

This does not work:

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)

Do you know where is the problem?

Upvotes: 0

Views: 786

Answers (1)

iklinac
iklinac

Reputation: 15738

if you look into allow_request function

self.rate is always None as you did not set it, hence request is allowed

Upvotes: 1

Related Questions