mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Thanks to @Roarster.
This commit is contained in:
parent
5670cb03bf
commit
0047a46020
|
@ -114,7 +114,7 @@ class WritableField(Field):
|
||||||
|
|
||||||
def __init__(self, source=None, read_only=False, required=None,
|
def __init__(self, source=None, read_only=False, required=None,
|
||||||
validators=[], error_messages=None, widget=None,
|
validators=[], error_messages=None, widget=None,
|
||||||
default=None):
|
default=None, blank=None):
|
||||||
|
|
||||||
super(WritableField, self).__init__(source=source)
|
super(WritableField, self).__init__(source=source)
|
||||||
|
|
||||||
|
@ -133,6 +133,7 @@ class WritableField(Field):
|
||||||
|
|
||||||
self.validators = self.default_validators + validators
|
self.validators = self.default_validators + validators
|
||||||
self.default = default or self.default
|
self.default = default or self.default
|
||||||
|
self.blank = blank
|
||||||
|
|
||||||
# Widgets are ony used for HTML forms.
|
# Widgets are ony used for HTML forms.
|
||||||
widget = widget or self.widget
|
widget = widget or self.widget
|
||||||
|
@ -467,6 +468,16 @@ class CharField(WritableField):
|
||||||
if max_length is not None:
|
if max_length is not None:
|
||||||
self.validators.append(validators.MaxLengthValidator(max_length))
|
self.validators.append(validators.MaxLengthValidator(max_length))
|
||||||
|
|
||||||
|
def validate(self, value):
|
||||||
|
"""
|
||||||
|
Validates that the value is supplied (if required).
|
||||||
|
"""
|
||||||
|
# if empty string and allow blank
|
||||||
|
if self.blank and not value:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
super(CharField, self).validate(value)
|
||||||
|
|
||||||
def from_native(self, value):
|
def from_native(self, value):
|
||||||
if isinstance(value, basestring) or value is None:
|
if isinstance(value, basestring) or value is None:
|
||||||
return value
|
return value
|
||||||
|
|
|
@ -393,6 +393,9 @@ class ModelSerializer(Serializer):
|
||||||
Creates a default instance of a basic non-relational field.
|
Creates a default instance of a basic non-relational field.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
|
|
||||||
|
kwargs['blank'] = model_field.blank
|
||||||
|
|
||||||
if model_field.null:
|
if model_field.null:
|
||||||
kwargs['required'] = False
|
kwargs['required'] = False
|
||||||
|
|
||||||
|
|
|
@ -121,3 +121,8 @@ class BlogPostComment(RESTFrameworkModel):
|
||||||
class Person(RESTFrameworkModel):
|
class Person(RESTFrameworkModel):
|
||||||
name = models.CharField(max_length=10)
|
name = models.CharField(max_length=10)
|
||||||
age = models.IntegerField(null=True, blank=True)
|
age = models.IntegerField(null=True, blank=True)
|
||||||
|
|
||||||
|
|
||||||
|
# Model for issue #324
|
||||||
|
class BlankFieldModel(RESTFrameworkModel):
|
||||||
|
title = models.CharField(max_length=100, blank=True)
|
||||||
|
|
|
@ -449,3 +449,52 @@ class ManyRelatedTests(TestCase):
|
||||||
}
|
}
|
||||||
|
|
||||||
self.assertEqual(serializer.data, expected)
|
self.assertEqual(serializer.data, expected)
|
||||||
|
|
||||||
|
|
||||||
|
# Test for issue #324
|
||||||
|
class BlankFieldTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
|
||||||
|
class BlankFieldModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = BlankFieldModel
|
||||||
|
|
||||||
|
class BlankFieldSerializer(serializers.Serializer):
|
||||||
|
title = serializers.CharField(blank=True)
|
||||||
|
|
||||||
|
class NotBlankFieldModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = BasicModel
|
||||||
|
|
||||||
|
class NotBlankFieldSerializer(serializers.Serializer):
|
||||||
|
title = serializers.CharField()
|
||||||
|
|
||||||
|
self.model_serializer_class = BlankFieldModelSerializer
|
||||||
|
self.serializer_class = BlankFieldSerializer
|
||||||
|
self.not_blank_model_serializer_class = NotBlankFieldModelSerializer
|
||||||
|
self.not_blank_serializer_class = NotBlankFieldSerializer
|
||||||
|
self.data = {'title': ''}
|
||||||
|
|
||||||
|
def test_create_blank_field(self):
|
||||||
|
serializer = self.serializer_class(self.data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
|
||||||
|
def test_create_model_blank_field(self):
|
||||||
|
serializer = self.model_serializer_class(self.data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
|
||||||
|
def test_create_not_blank_field(self):
|
||||||
|
"""
|
||||||
|
Test to ensure blank data in a field not marked as blank=True
|
||||||
|
is considered invalid in a non-model serializer
|
||||||
|
"""
|
||||||
|
serializer = self.not_blank_serializer_class(self.data)
|
||||||
|
self.assertEquals(serializer.is_valid(), False)
|
||||||
|
|
||||||
|
def test_create_model_not_blank_field(self):
|
||||||
|
"""
|
||||||
|
Test to ensure blank data in a field not marked as blank=True
|
||||||
|
is considered invalid in a model serializer
|
||||||
|
"""
|
||||||
|
serializer = self.not_blank_model_serializer_class(self.data)
|
||||||
|
self.assertEquals(serializer.is_valid(), False)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user