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
})
- How to import a json file to a Django model?
- Django – Overriding get_form to customize admin forms based on request
- Make Django return response as a "different filename"
- How to make follower-following system with django model
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]
- Deciding and implementing a trending algorithm in Django
- How to limit queryset/the records to view in Django admin site?
- Django – Function inside a model. How to call it from a view?
- Database table names with Django
- How to I hide my secret_key using virtualenv and Django?
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)
- Whole model as read-only
- How does Django serve media files?
- How show personalized error with get_object_or_404
- Make Django forms use comma as decimal separator
Source:stackexchange.com