gpt4 book ai didi

python - 在 django-tastypie 中获取经过身份验证的用户

转载 作者:行者123 更新时间:2023-11-28 22:57:47 26 4
gpt4 key购买 nike

我刚开始使用 Python 和 Django,并使用 tastypie 创建 RESTful API。

我需要根据经过身份验证的用户计算资源的字段,我打算覆盖资源中的 dehydrate_field 方法,但我不知道如何在 dehydrate_field 方法中获取经过身份验证的用户。

我正在使用 tastypie 的 ApiKeyAuthentication,我目前正在 URL 的查询字符串中传递身份验证参数,但我也希望能够在身份验证 header 中传递身份验证参数。

我想我应该能够自己从查询字符串或授权 header 中获取用户名,并找到用户,但我觉得它已经在某个地方的 tastypie 中实现了,但我做不到在文档中找到它。

下面是一些示例代码:

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>

我想知道 tastypie 是否有一些内置功能来获取经过身份验证的用户或根据查询字符串参数或 header 字段滚动我自己的正确方法。

最佳答案

我找到了问题的答案,获取我正在做的当前用户

user = bundle.request.user

感谢Aidan Ewen为我指明了正确的方向。

关于python - 在 django-tastypie 中获取经过身份验证的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com