diff options
| author | Tom Christie | 2013-01-28 07:30:28 +0000 | 
|---|---|---|
| committer | Tom Christie | 2013-01-28 07:30:28 +0000 | 
| commit | cb219fa04f6a4d4ae0d99920380416f62126b87d (patch) | |
| tree | 568184563a201d525d1906049d37618a3a1d0365 /docs/api-guide | |
| parent | 84a33b0a1f89047a24af6845600cccd982bc2baf (diff) | |
| download | django-rest-framework-cb219fa04f6a4d4ae0d99920380416f62126b87d.tar.bz2 | |
Example custom throttle. Fixes #300.
Diffstat (limited to 'docs/api-guide')
| -rw-r--r-- | docs/api-guide/throttling.md | 10 | 
1 files changed, 9 insertions, 1 deletions
| diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md index b03bc9e0..86b1fe8d 100644 --- a/docs/api-guide/throttling.md +++ b/docs/api-guide/throttling.md @@ -150,8 +150,16 @@ User requests to either `ContactListView` or `ContactDetailView` would be restri  # Custom throttles -To create a custom throttle, override `BaseThrottle` and implement `.allow_request(request, view)`.  The method should return `True` if the request should be allowed, and `False` otherwise. +To create a custom throttle, override `BaseThrottle` and implement `.allow_request(self, request, view)`.  The method should return `True` if the request should be allowed, and `False` otherwise.  Optionally you may also override the `.wait()` method.  If implemented, `.wait()` should return a recommended number of seconds to wait before attempting the next request, or `None`.  The `.wait()` method will only be called if `.allow_request()` has previously returned `False`. +## Example + +The following example will randomly throttle 1 in every 10 requests. + +    class RandomRateThrottle(throttles.BaseThrottle): +        def allow_request(self, request, view): +            return random.randint(1, 10) == 1 +  [permissions]: permissions.md | 
