【发布时间】:2013-01-20 15:11:04
【问题描述】:
我刚从 Python 和 Django 开始,并使用 sweetpie 创建一个 RESTful API。
我需要根据认证用户计算资源的一个字段,我打算在我的资源中重写dehydrate_field方法,但是我不知道如何在dehydrate_field方法中获取认证用户。
我正在使用tastepie的ApiKeyAuthentication,我目前正在URL的查询字符串中传递身份验证参数,但我也希望能够在Authentication header中传递身份验证参数。
我想我应该能够自己从查询字符串或授权标头中获取用户名,并找到用户,但我觉得它已经在某处的美味派中实现了,但我不能在文档中找到它。
这里有一些示例代码:
class MyModelResource(ModelResource):
calculated_field = fields.BooleanField(readonly=True)
class Meta:
queryset = MyModel.objects.all()
resource_name = 'mymodel'
authentication = ApiKeyAuthentication()
def dehydrate_calculated_field(self, bundle):
user = <get authenticated user somehow>
return <some boolean that's calculated depending on authenticated user>
我想知道 sweetpie 是否有一些内置功能来获取经过身份验证的用户,或者根据查询字符串参数或标头字段滚动我自己的正确方法。
【问题讨论】:
标签: python django authentication tastypie