mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Merge pull request #1834 from piotrjakimiak/master
Fix returning None when allow_none is True in CharField
This commit is contained in:
commit
2b47c6b700
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user