Consider a Django REST Framework API view with a throttle class set to limit requests to 5 per minute per user. What is the expected behavior when a user sends the 6th request within the same minute?
from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView from rest_framework.response import Response class FivePerMinuteThrottle(UserRateThrottle): rate = '5/minute' class ExampleView(APIView): throttle_classes = [FivePerMinuteThrottle] def get(self, request): return Response({'message': 'Request successful'})
Think about what HTTP status code is used when rate limits are exceeded.
When a user exceeds the throttle limit, Django REST Framework returns a 429 Too Many Requests response, blocking further requests until the throttle window resets.
Which of the following code snippets correctly sets a throttle rate of 10 requests per hour for a custom throttle class?
Remember the rate must be a string with a number and time unit separated by a slash.
The throttle rate must be a string like '10/hour'. Other formats cause syntax or runtime errors.
Given the following throttle class, users are not being limited to 3 requests per minute as intended. What is the cause?
from rest_framework.throttling import UserRateThrottle class ThreePerMinuteThrottle(UserRateThrottle): rate = '3/minute'
Check the data type of the rate attribute.
The rate attribute must be a string like '3/minute'. Using 3/minute without quotes causes a syntax error or unexpected behavior.
When using UserRateThrottle, how does Django REST Framework identify different users to apply rate limits?
Think about how authentication works in Django REST Framework.
UserRateThrottle uses the authenticated user object (request.user) to apply rate limits per user. If the user is anonymous, it falls back to IP-based throttling.
Given the following settings and code, what will be the response status code of the 4th request made by the same authenticated user within one minute?
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.UserRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '3/minute'
}
}
from rest_framework.views import APIView
from rest_framework.response import Response
class TestView(APIView):
def get(self, request):
return Response({'detail': 'Success'})Check the throttle rate and how many requests are allowed per minute.
The throttle rate is 3 requests per minute per user. The 4th request exceeds this limit and returns a 429 status code.