Documentation says that period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')
. I am curious if I can set period to something like 1/10min
?
Asked
Active
Viewed 1,942 times
7
-
1But the title says, once in ten minutes. So that means `10min` right? – Willem Van Onsem May 16 '18 at 12:09
-
@WillemVanOnsem yes, I need to block users from sending more that 1 request per ten minutes – pythad May 16 '18 at 12:12
2 Answers
11
Looking at the code and documentation you cannot do this 'out of the box'. But I do see the possibility described to make your own custom throttle based on one of the existing one's:
from rest_framework.throttling import AnonRateThrottle
class AnonTenPerTenMinutesThrottle(AnonRateThrottle):
def parse_rate(self, rate):
"""
Given the request rate string, return a two tuple of:
<allowed number of requests>, <period of time in seconds>
So we always return a rate for 10 request per 10 minutes.
Args:
string: rate to be parsed, which we ignore.
Returns:
tuple: <allowed number of requests>, <period of time in seconds>
"""
return (10, 600)

The Pjot
- 1,801
- 1
- 12
- 20
4
Basically what XY said is true but it might not work because you still have to put the rate like this:
from rest_framework.throttling import AnonRateThrottle
class AnonTenPerTenMinutesThrottle(AnonRateThrottle):
rate = '1/s' # <<<<< This line is very important
# You just can enter any rate you want it will directly be overwritten by parse_rate
def parse_rate(self, rate):
"""
Given the request rate string, return a two tuple of:
<allowed number of requests>, <period of time in seconds>
So we always return a rate for 10 request per 10 minutes.
Args:
string: rate to be parsed, which we ignore.
Returns:
tuple: <allowed number of requests>, <period of time in seconds>
"""
return (10, 600) # 10 Requests per 600 seconds (10 minutes)

Henrik
- 828
- 8
- 34