自定义ScopedRateThrottle中的访问URL参数变量

时间:2018-10-14 11:07:52

标签: django django-rest-framework

我正在使用Django Framework和Django REST Framework。

我有一个端点需要使用(user, token)对来限制,其中user是发出请求的用户,token是{{1}中指定的URL变量}

urls.py

我创建了一个自定义ScopedRateThrottle来完成此操作:

url(r'^api/v2/(?P<token>\w+)/action$', ActionEndpoint.as_view())

问题:在这种情况下,如何从class CustomThrottle(ScopedRateThrottle): rate = '2/day' def get_cache_key(self, request, view): user_id = request.user.pk token = (?????????) identity = "%s_%s" % (user_id, token) cache_key = self.cache_format % { 'scope': self.scope, 'ident': identity } return cache_key 对象中检索token变量?

1 个答案:

答案 0 :(得分:1)

您应该使用view对象从url()模式中检索参数:

token = view.kwargs['token']