DecoratorsΒΆ
The decorators made available as instance methods of the Limiter
instance are
limit()
There are a few ways of using this decorator depending on your preference and use-case.
- Single decorator
The limit string can be a single limit or a delimiter separated string
@limit("100/day;10/hour;1/minute") def my_view(request) ...
- Multiple decorators
The limit string can be a single limit or a delimiter separated string or a combination of both.
@limit("100/day") @limit("10/hour") @limit("1/minute") def my_view(request): ...
- Custom keying function
By default rate limits are applied on per remote address basis. You can implement your own function to retrieve the key to rate limit by.
def my_key_func(request): ... @limit("100/day", my_key_func) def my_view(request): ...
Note
The key function must accept one argument which is a
django.http.HttpRequest
object- Dynamically loaded limit string(s)
There may be situations where the rate limits need to be retrieved from sources external to the code (database, remote api, etc...). This can be achieved by providing a callable (which takes a single parameter - the
django.http.HttpRequest
object) to the decorator. The callable should return a rate limit string in the Rate limit string notation.from django.conf import settings def rate_limit_from_config(request): return settings.CUSTOM_LIMIT @limit(rate_limit_from_config) def my_view(request): ...
Danger
The provided callable will be called for every request on the decorated route. For expensive retrievals, consider caching the response.
exempt()
- This decorator simply marks a view as being exempt from any rate limits.