文档:https://www.django-rest-framework.org/api-guide/throttling/
实现步骤有三:
1、项目的settings.py中配置REST_FRAMEWORK
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
2、views.py中导包
from rest_framework.throttling import AnonRateThrottle,UserRateThrottle
3、配置到viewsets中
throttle_classes = (AnonRateThrottle,UserRateThrottle)