Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DRF image field does not accept null values

Django DRF image field does not accept null values.

My model consists of image = models.ImageField(upload_to='uploads/', blank=True, null=True)

If I upload a file there's no problem. But, if I leave it blank it gives the following error:

The submitted data was not a file. Check the encoding type on the form.

I want to make the image field optional So I set null=True and blank=True. But this does not seem to work.

like image 858
Nisha Manu Avatar asked Feb 24 '26 22:02

Nisha Manu


1 Answers

In DRF your model passes through serializer add this to your serializer:

class ImagePostSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(required=False)
    class Meta:
        model = models.ImagePost
like image 106
enes islam Avatar answered Feb 26 '26 11:02

enes islam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!