Timmerop
Timmerop

Reputation: 35

Django Rest Framework: Updating model with ImageField without updating the image

Using Django Rest Framework I am trying to update a model with an image field. When I try to update the other fields on the model I get this error:

{"image": ["No file was submitted. Check the encoding type on the form."]}

Here is a simply idea of the serializer. The image field it returns on a GET call just has the file name.

class ModelWithImageSerializer(serializers.ModelSerializer):
    image = serializers.ImageField('image', required=False)
    class Meta:
        model = models.Level
        fields = ('id','name', 'image')

How do I update my model without resubmitting the file?

Upvotes: 2

Views: 3336

Answers (3)

Rohit Br
Rohit Br

Reputation: 1

Make the ImageField as read_only then it will work but when you have post method , then it won't work.

Upvotes: 0

ybendana
ybendana

Reputation: 1643

I was having the same issue with a FileField. I had to delete the FileField property on the PUT request so that it won't override the stored value. Just make sure that blank=True on the Model so that it's an optional field. I'm using DRF 2.4.4.

Upvotes: 0

miki725
miki725

Reputation: 27861

Django REST allows to submit partial PATCH requests (docs). Just make sure to use an UpdateAPIView (or variant of this) which automatically allows this. The idea of partial updates is that they do not require you to submit all model fields which will accomplish the behavior you need.

Upvotes: 3

Related Questions