from rest_framework.throttling import UserRateThrottle
class RockyRateThrottle(UserRateThrottle):
scope = 'rocky'
settings.py
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES':{
'anon': '2/day', # For Anonymous user
'user': '5/hour', # For Registred user
'rocky': '3/minute' # For register but specific user
}
}
views.py
from rest_framework.throttling import AnonRateThrottle, UserRateThrottle
from api.throttling import RockyRateThrottle
class StudentModelViewSet(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
authentication_classes=[SessionAuthentication]
permission_classes=[IsAuthenticatedOrReadOnly]
# throttle_classes = [AnonRateThrottle, UserRateThrottle]
throttle_classes = [AnonRateThrottle, RockyRateThrottle] # this is working for 'anon' and 'Rocky'
2条答案
按热度按时间jjhzyzn01#
在这里,我为特定用户创建了Throttling
throttling.py
settings.py
views.py
jhdbpxl92#
要对视图全局应用throttle,您可以使用相同的键。这里的想法是对每个视图使用相同的键。这意味着对于所有请求,它将使用相同的键来获取请求计数数据
这将节流每个视图.相同的作为你正在寻找