Django Rest框架没有找到调节范围?

2024-06-28 11:01:46 发布

您现在位置:Python中文网/ 问答频道 /正文

根据http://www.django-rest-framework.org/api-guide/throttling/中的示例推断,我在DRF设置中添加了以下设置:

REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
    'rest_framework.throttling.AnonRateThrottle',
    'rest_framework.throttling.UserRateThrottle',
    'project.api.throttles.AppEventRateThrottle',
),

'DEFAULT_THROTTLE_RATES': { # 86,400 seconds in a day
    'app_events': '10000/day',
    'anon': '10000/day',
    'user': '10000/day',
},
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.BasicAuthentication',
    'rest_framework.authentication.TokenAuthentication',
),
'EXCEPTION_HANDLER': 'project.api.exception_handler.custom_exception_handler',
}

这是一个简单的AppEventRateThrottle类,位于project.api.throttles

^{pr2}$

我尝试限制的基于简单函数的API视图:

from project.api.throttles import AppEventRateThrottle
@api_view(['POST'])
@throttle_classes([AppEventRateThrottle])
def grouped_event_create(request):
    return Response("Hello, world!")

但是,每次我调用这个API时

  File "/usr/local/lib/python3.4/dist-packages/rest_framework/throttling.py", line 94, in get_rate
raise ImproperlyConfigured(msg)
 django.core.exceptions.ImproperlyConfigured: No default throttle rate set for 'app_events' scope

似乎在DEFAULT_THROTTLE_RATES字典中找不到'app\'events'键,但它定义得很清楚。在


Tags: djangoprojectrestapiappdefaultframeworkevents