1

I have created a Custom user Model for my project, using Django 3.2. To POST/PUT the details in the user I am using restframework.

But while giving the data for "PUT" operation to change some change I am getting the error as JSON parse error - Expecting value: line 1 column 1 (char 0)

def update_masteruser(request,pk):
    try: 
        user = MasterUser.objects.get(pk=pk) 
    except MasterUser.DoesNotExist: 
        return JsonResponse({'message': 'The User does not exist'}, status=status.HTTP_404_NOT_FOUND) 
 
    if request.method == "PUT":
        user_data = JSONParser().parse(request)  #problem seems to be on this line
        serializer = RegisterUserSerializer(user, data=user_data)
        if serializer.is_valid(): 
            serializer.save() 
            return JsonResponse(serializer.data) 
        return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

alternate method, this throws 'WSGIRequest' object has no attribute 'data':

def update_masteruser(request,pk):
    try: 
        user = MasterUser.objects.get(pk=pk) 
    except MasterUser.DoesNotExist: 
        return JsonResponse({'message': 'The User does not exist'}, status=status.HTTP_404_NOT_FOUND) 
    if request.method == "PUT":
        serializer = MasterUserSerializer(user, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "updated"
            return Response(data=data)
        return Response(serializer.data)

serializers.py

class MasterUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = MasterUser
        fields = "__all__"

I searched the error cause, but couldnt find the reason on it. Why cant it parse the data from request?

Ankzious
  • 311
  • 3
  • 18
  • `user_data = JSONParser().parse(request.data)`? – Brian Destura Jul 14 '21 at 06:14
  • To parse the request. I tried a alternate as well, updated in my post. – Ankzious Jul 14 '21 at 06:17
  • 1
    Is it intentional that the function is not decorated with `@api_view`? – Brian Destura Jul 14 '21 at 06:20
  • If not, try to get your data using [this](https://stackoverflow.com/questions/4994789/django-where-are-the-params-stored-on-a-put-delete-request) since you are using `PUT` – Brian Destura Jul 14 '21 at 06:24
  • I actually kept it disabled for testing, but now enabled and tried again. It is giving the response with the data which is changed. But there is no change in value when looked into admin login. – Ankzious Jul 14 '21 at 06:44
  • To check that can you show your serializer? – Brian Destura Jul 14 '21 at 07:10
  • It was a simple Modelserialzier. I edit the question, added serializer. – Ankzious Jul 14 '21 at 07:19
  • Test as in localhost? then yes. If its in test database, then how could I make in available to my database or in frontend with the updated data? – Ankzious Jul 14 '21 at 07:28
  • Sorry disregard what I said. How about you get the result of `save`? Like `master_user = serializer.save()` and check if it created the object you wanted – Brian Destura Jul 14 '21 at 07:31
  • sorry got a bit late on response. I printed on ```master_user```, it gives a blank output. but in postman there is a response ```{ "firstname": "blabla", "is_member": false, "is_subscription": false, "category": "test123" }```, which is for ```serializer.data``` – Ankzious Jul 14 '21 at 08:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234848/discussion-between-bdbd-and-ankzious). – Brian Destura Jul 14 '21 at 08:53

2 Answers2

0

Refer to This Documentation given below!!

https://www.django-rest-framework.org/api-guide/parsers/#how-the-parser-is-determined

0

I tried a alternate method. Instead of using .save , I use .update() and it worked. Something like below

serializer.update(user,serializer.validated_data)

Thanks @bdbd for you response and time! Appreciated

Ankzious
  • 311
  • 3
  • 18