0
votes

I am trying to update the first_name field for a user using a JWT token for authentication, for some reason when I am doing it in a different table where there is no username and password field ,I can easily do it and update the details using JWT token. But when i do it in Django user model default table,it keep asking me to include the username and password on the request(even thought I don't want to update them). here is the error I am getting when sending the request using postman

{
    "username": [
        "This field is required."
    ],
    "password": [
        "This field is required."
    ]
}

even though I have the authorisation header set right and it works with any other request, but not when updating user details, I have included all the necessary and default authentication classes. any help is appreciated, been trying to solve this small thing for 4 days now.

curl command not working

curl -H "Authorization: JWT <token>" -X PUT  http://localhost:8000/user/3/ -d '{"first_name":"curl_test"}'

curl command working

curl -H "Authorization: JWT <token>" -X GET  http://localhost:8000/user/3/

NOTICE THE DIFFERENT method. my views.py (UserDetails class)

class UserDetail(APIView):
    permission_classes = (IsOwner, IsAuthenticated)
    """
    Retrieve, update or delete a user instance.
    """

    def get_object(self, pk):
        try:
            return User.objects.get(pk=pk)
        except User.DoesNotExist:
            raise Http404


    def get(self, request, pk, format=None):
        user = self.get_object(pk)
        serializer = UserSerializer(user)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        user = self.get_object(pk)
        serializer = UserSerializer(user, data=request.data)
        if serializer.is_valid():
            serializer.save()
            user = Profile.objects.get(id=pk)
            user.profile.updated = timezone.now()
            user.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        user = self.get_object(pk)
        user.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

my settings.py

REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',

    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser',
        'rest_framework.parsers.JSONParser',

    )
}

JWT is fully working ,when sending an old Token on the request I am getting Signature expired, but when I put the latest token I get the username/passwords are required fields.

1
try to use patch method - Brown Bear
Same error. JWT says i have to include the username and password and ignoring the token on the header - Abdul
add json of the data you send to the server, - Brown Bear
You want me to include the curl command i sent? I will do that once my laptop finishes updating - Abdul
yes add it to your question, please - Brown Bear

1 Answers

1
votes

Your user serializer is enforcing all fields data, use partial=True to allow partial updates:

serializer = UserSerializer(user, data={'username': u'test'}, partial=True)