M
M
marselabdullin2020-07-25 16:03:45
Django
marselabdullin, 2020-07-25 16:03:45

400 error when passing blob file via rest?

The file is transferred from vue.js, the picture is cropped there and transferred in blob format, however, the field type of the model into which the blob is written is imagefield, and I suppose this causes a 400 error, but in the serializer I convert blob to imagefield, but apparently an error occurs before this point, since neither views nor serializers write anything to the logs. What can be done in such a case?

views.py:

def create(self, request, *args, **kwargs):
        if not request.data:
            logger.error("Not request.data")
            return response.Response({"result": "data can`t be a blanks"}, status.HTTP_400_BAD_REQUEST)
        logger.error(request.data)
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        return response.Response(serializer.data, status=status.HTTP_201_CREATED)


serializers.py
def create(self, validated_data):
        logger.error(validated_data)
        user = User.objects.get(email=validated_data['email'])
        user.update(**validated_data)

        with io.BytesIO(self.context.get('view').request.FILES['image']) as stream:
            django_file = File(stream)
            user.image.save('{}_profile_image'.format(user.email), django_file)
            

        return user.save()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-07-25
@bacon

Why so many perversions? The idea is that the following code should work

user.image = self.context.get('view').request.FILES['image']

return user.save()what else is this? It is necessary to return the object itself, and not the result of the work of the save function
And by mistake, debug, catch the places where it occurs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question