Object of type 'AuthToken' is not JSON serializable
See original GitHub issueI’m getting the above error when creating token, here’s the code:
` from rest_framework import generics, permissions from rest_framework.response import Response from knox.models import AuthToken from .serializers import UserSerializer, RegisterSerializer
class RegisterAPI(generics.GenericAPIView): serializer_class = RegisterSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)
})
`
what am I doing wrong here
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
Object of type 'AuthToken' is not JSON serializable
1. Well you will need to serialize the AuthToken as well, like you did with the user (or pass an attribute of that...
Read more >Django : Object of type 'AuthToken' is not JSON serializable
Django : Object of type ' AuthToken' is not JSON serializable [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Django ...
Read more >Object of type Token is not JSON serializable-django
when I wanna login I get an error. class AuthAPIView(APIView): def post(self, request, format=None): data = request.data username = data.get(' ...
Read more >Object of type 'AuthToken' is not JSON serializable - iTecNote
I'm getting the above error when creating token, here's the code: from rest_framework import generics, permissions from rest_framework.response import ...
Read more >Django deserialize json to model
Object of type is not JSON serializable django-rest framework ... James1345 / django-rest-knox. Object of type 'AuthToken' is not JSON serializable #177. Object ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
AuthToken.objects.create()
returns a tuple of 2 values: the model instance and the token itself. See https://github.com/James1345/django-rest-knox/blob/05f218f1922999d1be76753076cf8af78f134e02/knox/models.py#L23 You only need to pass the token in your response, not the all tuple.Something like this would do:
one other way is use this
"token": AuthToken.objects.create(user)[1]