Solution 1: With the @override_default()
wrapper
You want to apply a wrapper function to the viewset
that you wish to override with this value:
from rest_framework.throttling import UserRateThrottle
class UserViewSet(viewsets.ViewSet):
@api_view(['PUT'])
@throttle_classes([UserRateThrottle])
@override_settings(REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['user'] = "2/day")
def update(self, request, pk=None):
...
Add the wrapper before any view or viewset you wish to apply this overridden customs value.
You also want this in your settings.py
:
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '13500/day'
}
}
Solution 2: Custom throttling class
However, if you want different throttling rates for when you're in a test environment, perhaps try the following in settings.py
:
TEST = True ## <-- Set to False when not in testing!!
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.CustomUserRateThrottle'
)
}
And designate your own custom throttling class:
from django.conf import settings
class CustomUserRateThrottle(throttling.UserRateThrottle):
if settings.TEST:
THROTTLE_RATES = 'DEFAULT_THROTTLE_RATES': {
'user': '13500/day',
}
else:
THROTTLE_RATES = 'DEFAULT_THROTTLE_RATES': {
'user': '2/day',
}
return settings.TEST # <-- Custom throttling classes must always return either True or False, so this makes sense.
Solution 3: directly in your "testing shell":
Include your custom wrapper as we defined above but this time in your test_something()
method in test.py
:
from django.conf import settings
from django.test import TestCase
class TestCase(TestCase):
def test_something(self):
with self.settings(REST_FRAMEWORK = ['DEFAULT_THROTTLE_RATES']['user'] = '13500/day'):
# START TESTING HERE WITH TEST SETTINGS