Django REST如何设置节流周期以在10分钟内允许一个请求?

时间:2018-05-16 12:08:51

标签: python django django-rest-framework throttling

文档说该句号应该是以下之一:('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')。我很好奇我是否可以将句点设置为1/10min

1 个答案:

答案 0 :(得分:1)

查看codedocumentation,您无法开箱即用。但我确实看到了根据现有的custom throttle创建自己的enter image description here的可能性:

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)