我使用rest framework django。我想用他的令牌恢复用户。该令牌必须通过邮寄请求发送
class GetUser(generics.ListCreateAPIView):
serializer_class = serializers.UserBasicSerializer
def get_queryset(self):
return models.Member.objects.filter()
def post(self, request, *args, **kwargs):
user = Token.objects.get(*args, **kwargs).user
我收到此错误消息
rest_framework.authtoken.models.MultipleObjectsReturned: get() returned more than one Token -- it returned 2!
感谢
答案 0 :(得分:0)
使用:
user = Token.objects.filter(*args, **kwargs)
if user.exists():
user = user.last().user
答案 1 :(得分:0)
您的问题的答案在文档中。看看这里:http://www.django-rest-framework.org/api-guide/requests/#user
基本上,您只需要从方法用户获取请求。例如:
def api_name_of_api(request):
user_data = request.user # Get username
user_data = request.user.id # Get user id
...