[Backport] Fixes CharField behaviour and introduces proper validation for ModelField

remotes/origin/issue/4217/improving-mail-design
Alejandro Alonso 2016-10-21 09:28:02 +02:00 committed by David Barragán Merino
parent 1c5557f330
commit a3f88db4d1
1 changed files with 8 additions and 2 deletions

View File

@ -467,6 +467,11 @@ class ModelField(WritableField):
"type": self.model_field.get_internal_type() "type": self.model_field.get_internal_type()
} }
def validate(self, value):
super(ModelField, self).validate(value)
if value is None and not self.model_field.null:
raise ValidationError(self.error_messages['invalid'])
##### Typed Fields ##### ##### Typed Fields #####
@ -512,8 +517,9 @@ class CharField(WritableField):
self.validators.append(validators.MaxLengthValidator(max_length)) self.validators.append(validators.MaxLengthValidator(max_length))
def from_native(self, value): def from_native(self, value):
if isinstance(value, six.string_types) or value is None: if value in validators.EMPTY_VALUES:
return value return ""
return smart_text(value) return smart_text(value)
def to_native(self, value): def to_native(self, value):