Hey guys, hope you're all good!
I'm having problems with the serialization of empty strings on DRF.
I have a model with two fields that are defined as blank = True and null = True, date and message.
models.py
class ContactUs(models.Model):
email = models.CharField(_('email'), max_length=100)
date = models.DateField(_('event date'), blank=True, null=True)
message = models.CharField(_('message'), blank=True, null=True, max_length=2048)
In my serializer, I defined these fields as required = False and allow_null = True.
serializers.py
class ContactUsSerializer(serializers.ModelSerializer):
message = serializers.CharField(required=False, allow_null=True)
date = serializers.DateField(required=False, allow_null=True)
class Meta:
model = ContactUs
fields = '__all__'
When the frontend does not send these fields or send them = null everything works 100%, however when it sends an empty string "" the serializer raises a validation error. I saw that I can handle this by defining to_internal_value (), but I think there may be a simpler way.
Does anyone have any other tips on how to solve this?
Shouldn't have an allow_empty=True option?
Thx,
Felipe