question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object of type 'AuthToken' is not JSON serializable

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

7reactions
johnrazcommented, Apr 24, 2019

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:

    ...
    _, token = AuthToken.objects.create(user)
    return Response({
        "user": UserSerializer(user, context=self.get_serializer_context()).data,
        "token": token
    })
1reaction
radeshfcommented, Jun 30, 2019

one other way is use this

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found