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

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/f4de3f84-6192-4c28-ae60-f5d62001bc6a%40googlegroups.com.

Reply via email to