drf-extra-fields
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")?
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
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
Sorry for the late response. Yes, setting allow_null
to True
made the trick!
Thanks!