Merge pull request #1834 from piotrjakimiak/master

Fix returning None when allow_none is True in CharField
This commit is contained in:
Tom Christie 2014-09-05 13:57:34 +01:00
commit 2b47c6b700
2 changed files with 18 additions and 2 deletions

View File

@ -475,8 +475,12 @@ class CharField(WritableField):
if isinstance(value, six.string_types):
return value
if value is None and not self.allow_none:
return ''
if value is None:
if not self.allow_none:
return ''
else:
# Return None explicitly because smart_text(None) == 'None'. See #1834 for details
return None
return smart_text(value)

View File

@ -1004,6 +1004,18 @@ class BooleanField(TestCase):
self.assertFalse(BooleanRequiredSerializer(data={}).is_valid())
class ModelCharField(TestCase):
"""
Tests for CharField
"""
def test_none_serializing(self):
class CharFieldSerializer(serializers.Serializer):
char = serializers.CharField(allow_none=True, required=False)
serializer = CharFieldSerializer(data={'char': None})
self.assertTrue(serializer.is_valid())
self.assertIsNone(serializer.object['char'])
class SerializerMethodFieldTest(TestCase):
"""
Tests for the SerializerMethodField field_to_native() behavior