gpt4 book ai didi

python - Django : TokenAuthentication, 设置端点权限

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:10 26 4
gpt4 key购买 nike

我正在使用 Django Rest Framework 来构建我的 API,而且我对它还很陌生。

我设置了一个 TokenAuthentication 方法,现在我尝试根据此 token 过滤查询集的结果。

基本上,我有一个 GET 端点(假设为“/wallet”),并且我希望/wallet 端点提供发送查询的特定用户的钱包。

我的方法是在 ViewSet 中重新定义 get_queryset 方法,但我不知道如何获取 token 以及如何过滤结果。

此外,不应允许任何匿名用户访问该端点。

这是我的 ViewSet,我想我需要在这里进行一些自定义:

class WalletViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows wallets to be viewed or edited.
"""
queryset = Wallet.objects.filter()
serializer_class = WalletSerializer

最佳答案

My approach was to redefine the get_queryset method in my ViewSet but I can't figure out how to get the token, and how to filter the results.

Django REST 框架 TokenAuthentication 链接到用户。因此,我建议您通过 self.request.user

过滤应在 View 中可用的用户

Also, anynomous users shouldn't be allowed to access that endpoint.

检查文档的权限部分。

关于python - Django : TokenAuthentication, 设置端点权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35060741/

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