Home > OS >  Django DRF image field does not accept null values
Django DRF image field does not accept null values

Time:03-29

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.

CodePudding user response:

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

CodePudding user response:

I got the solution. I was using a different serializer to create an instance of that model. I removed the serializer and now it is working.

  • Related