0

I have created an api for uploading 'profilepicture' for an existing user.

Models.py

  class ProfilePicture(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile_picture')
    profile_pic_path = models.FileField(
    upload_to=path_and_rename,
    max_length=254, blank=True, null=True
  ) class Meta:
    db_table = "profile_picture"

Serializer.py

class ProfilePictureSerializer(serializers.ModelSerializer):

  class Meta:
    model = ProfilePicture
    fields = '__all__'

Views.py

class ProfilePictureViewSet(viewsets.ModelViewSet):
  queryset = ProfilePicture.objects.all()
  serializer_class = ProfilePictureSerializer

  def create(self, request, *args, **kwargs):
    serializer = self.get_serializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        custom_data = {
            "status": True,
            "message": 'Successfully uploaded your profile picture.',
            "data": serializer.data
        }
        return Response(custom_data, status=status.HTTP_201_CREATED)
    else:
        custom_data = {
            "status": False,
            "message": serializer.errors,
        }
        return Response(custom_data, status=status.HTTP_200_OK)

The api is working fine using 'Django REST framework' UI.

While using postman i'm getting this error.

enter image description here

How can i solve this?

Aryan VR
  • 189
  • 1
  • 12

1 Answers1

0

Please try to pass content-type = multipart/form-data if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, Otherwise, use application/x-www-form-urlencoded. ref: here

nilay shah
  • 61
  • 3
  • On passing 'multipart/form-data' as 'Content-Type' I'm getting a new error - ' "detail": "Multipart form parse error - Invalid boundary in multipart: None" '. But it worked when I unchecked the 'Content-Type'. Is that ok to uncheck the box? – Aryan VR Jun 04 '21 at 02:57