Reputation: 636
i have:
class Phone(models.Model):
code = models.CharField(max_length=5)
verification = models.UUIDField(default=uuid.uuid4, unique=True)
In my viewset I must limit the number of requests to my endpoint from device by his verification.
class MyViewSet(viewsets.GenericViewSet):
permission_classes = (permissions.AllowAny,)
throttle_classes = (UserPhoneThrottle,)
serializer_class_map = {
...,
'verify': serializers.VerifyCodeSerializer,
}
I try to write my custom throttle:
class UserPhoneThrottle(throttling.SimpleRateThrottle):
def get_cache_key(self, request, view):
verification_id = request.data.get('verification_id')
if not verification_id:
return None
else:
return self.cache_format % {
'scope': self.scope,
'ident': verification_id
}
In my settings:
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'app.throttling.UserPhoneThrottle',
),
'DEFAULT_THROTTLE_RATES': {
...?
}
}
I ask to help or assist correctly to finish the given logic and to prompt what is this an error:
django.core.exceptions.ImproperlyConfigured: You must set either
.scope
or.rate
for 'UserPhoneThrottle' throttle
I will be very grateful for the help. Thank you!)
Upvotes: 1
Views: 1030
Reputation: 88689
The error is pretty straightforward,
You should either set something like this in settings.py
as,
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'app.throttling.UserPhoneThrottle',
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
OR
set .scope
in UserPhoneThrottle
in as,
class UserPhoneThrottle(throttling.SimpleRateThrottle):
scope = 'user'
# your code
Upvotes: 3