Merge pull request #1074 from diox/choicefield-empty-value

Make ChoiceField.from_native() follow IntegerField behaviour on empty values
This commit is contained in:
Tom Christie 2013-08-29 09:20:38 -07:00
commit cf2033f8bf
2 changed files with 13 additions and 0 deletions

View File

@ -514,6 +514,11 @@ class ChoiceField(WritableField):
return True return True
return False return False
def from_native(self, value):
if value in validators.EMPTY_VALUES:
return None
return super(ChoiceField, self).from_native(value)
class EmailField(CharField): class EmailField(CharField):
type_name = 'EmailField' type_name = 'EmailField'

View File

@ -688,6 +688,14 @@ class ChoiceFieldTests(TestCase):
f = serializers.ChoiceField(required=False, choices=self.SAMPLE_CHOICES) f = serializers.ChoiceField(required=False, choices=self.SAMPLE_CHOICES)
self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + self.SAMPLE_CHOICES) self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + self.SAMPLE_CHOICES)
def test_from_native_empty(self):
"""
Make sure from_native() returns None on empty param.
"""
f = serializers.ChoiceField(choices=self.SAMPLE_CHOICES)
result = f.from_native('')
self.assertEqual(result, None)
class EmailFieldTests(TestCase): class EmailFieldTests(TestCase):
""" """