django休息框架 - 添加到views.obtain_auth_token [英] django rest framework - adding to views.obtain_auth_token
问题描述
/ api-token-auth /
并获取令牌。 url(r'^ api-token-auth /',token_views.obtain_auth_token)
除了令牌之外,我想要获得与返回的令牌相关的 User
对象。
如何覆盖/添加此视图并返回实际的User对象?
您可以在这里找到相关视图:
假设您已经创建了一些用户序列化程序,您基本上可以使用用户
在那里实例,并将其推入您的UserSerializer。然后将其添加到响应中,如下所示。
...
user_serializer = UserSerializer(user)
返回响应({'token':token .key,'user':user_serializer.data}
I have implemented Token Authentication with django rest framework and I can post username and password to /api-token-auth/
and get the token.
url(r'^api-token-auth/', token_views.obtain_auth_token)
In addition to the token, I want to get the User
object related to the returned token.
How can I override/add to this view and also return the actual User object?
You can find the relevant view here:
Assuming you've created some sort of User serializer already, you can basically take the user
instance there and shove it into your UserSerializer. then add it to the response, something like the below.
...
user_serializer = UserSerializer(user)
return Response({'token': token.key, 'user': user_serializer.data})
这篇关于django休息框架 - 添加到views.obtain_auth_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!