小编Car*_* II的帖子

Django Rest Framework 找不到节流范围?

根据http://www.django-rest-framework.org/api-guide/throtdling/中的示例进行推断,我将以下设置添加到我的 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',
}
Run Code Online (Sandbox Code Playgroud)

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

from rest_framework.throttling import AnonRateThrottle

class AppEventRateThrottle(AnonRateThrottle):
     scope = 'app_events'
Run Code Online (Sandbox Code Playgroud)

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

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

然而,每次我进行这个 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 …
Run Code Online (Sandbox Code Playgroud)

python django rest django-rest-framework

8
推荐指数
1
解决办法
3048
查看次数

标签 统计

django ×1

django-rest-framework ×1

python ×1

rest ×1