django rest framework - adding to views.obtain_auth_token

3k views Asked by At

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?

1

There are 1 answers

0
Alex T On BEST ANSWER

You can find the relevant view here:

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/authtoken/views.py#L21

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})