[Fixed]-Object of type 'AuthToken' is not JSON serializable

46👍

The Token.objects.create returns a tuple (instance, token). So in order to get token use the index 1

"token": AuthToken.objects.create(user)[1]

15👍

Better way is use this method in python

_, token = AuthToken.objects.create(user)
return Response({
    "user": UserSerializer(user, context=self.get_serializer_context()).data,
    "token": token
})
👤Radesh

1👍

This particular error occurs because the Token.objects.create returns a tuple (instance, token). just use the second position [1] by using instead of former

"token": AuthToken.objects.create(user)[1]

0👍

If you’re using Django rest framework auth token do this:

from rest_framework.authtoken.models import Token

...
# The token will be in token variable, created variable will be a bool
token, created = Token.objects.get_or_create(user=request.user)

# Object of type Token is not JSON serializable, so convert token to string
token = str(token)


Leave a comment