drf-extra-fields icon indicating copy to clipboard operation
drf-extra-fields copied to clipboard

Why null values are not supported to remove a Base64 field's value ("This field may not be null")?

Open Genarito opened this issue 2 years ago • 1 comments

Hi! Thanks for this lib, It solved a lot of problems. But I realized that if I have the following field in my model:

class MyModel(models.Model):
    img = models.ImageField(null=True, blank=True)

And I use Base64 serialization:

class MySerializer(serializers.ModelSerializer):
    img = Base64ImageField(allow_empty_file=False, required=True)
    # ...

If I send { img: null } it throws This field may not be null error during validation. Also, I discover that sending an empty string sets the field to null. Is there a reason for that or It's just a bug?

Thanks in advance! <3

Genarito avatar May 12 '22 13:05 Genarito

Hi, thank you for reporting this.

If I send { img: null } it throws This field may not be null error during validation.

Do you get the same error if you add allow_null=True to for field definition? Base64ImageField(allow_empty_file=False, required=True, allow_null=True)

Also, I discover that sending an empty string sets the field to null. Is there a reason for that or It's just a bug?

Accpording to this line EMPTY_VALUES = (None, "", [], (), {}), empty string accepted as an empty value so it is intended. https://github.com/Hipo/drf-extra-fields/blob/master/drf_extra_fields/fields.py#L39

gokselcoban avatar Jun 27 '22 15:06 gokselcoban

Sorry for the late response. Yes, setting allow_null to True made the trick!

Thanks!

Genarito avatar Nov 14 '22 23:11 Genarito