我正在使用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
变量?
答案 0 :(得分:1)
您应该使用view
对象从url()
模式中检索参数:
token = view.kwargs['token']