3

I need to update my user in REST framework

views.py

class UserUpdate(APIView):
    permission_classes = (permissions.IsAuthenticated,)

    def post(self,request):
        user=User.objects.get(id=request.user.id)
        try:
            user_serializer=UserSerializer(request.user,data=request.data, partial=True)
            if user_serializer.is_valid():
                user_serializer.save()
                return Response(user_serializer.data, status=status.HTTP_201_CREATED)
            else:
                return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except:
            return JsonResponse({'status':0,'message':'Error on user update'})

serializers.py

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'first_name', 'last_name', 'avatar']

models.py

class User(AbstractUser):
    fb_userid = models.CharField(max_length=256)
    avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)

response: DETAIL: Key (username)=() already exists.

enter image description here

1
  • Show us your full view class Commented Sep 29, 2018 at 11:44

3 Answers 3

2
   def post(self,request):
        user_serializer=UserSerializer(request.user, data=request.data, partial=True)
        if user_serializer.is_valid():
            user_serializer.save()
            return Response(user_serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Sign up to request clarification or add additional context in comments.

6 Comments

{ "id": 2, "first_name": "", "last_name": "", "avatar": null } please check postman image
The url you are asking is update_user,is this url connect to this posyt method?
Yes i'm trying to post data with this url
but you still get user unchanged? can you print(request.data, user_serializer.validated_data) inside if user_serializer.is_valid() and post it to question
<QueryDict: {' name': ['"first_name"\r\n\r\nTarun\r\n------WebKitFormBoundaryeZV6EJv4GBVHzMQS--\r\n'], '------WebKitFormBoundaryeZV6EJv4GBVHzMQS\r\nContent-Disposition: form-data': ['']}>
|
1

@YKH is right this code is good there may error in your POST data.

In your image, you are passing two parameters in Header. It could possible you are passing the wrong header. Content-Type should not be for form-data

Comments

1

I found this post where someone has a similar problem as you: Django Rest Framework unable to parse multipart/form data

It seems on your picture that you are putting something into Headers tab. Postman is taking care of that for you, so you shouldn't define anything there. Could you try again without setting anything in the headers?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.