mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 10:03:57 +03:00
Merge pull request #1074 from diox/choicefield-empty-value
Make ChoiceField.from_native() follow IntegerField behaviour on empty values
This commit is contained in:
commit
cf2033f8bf
|
@ -514,6 +514,11 @@ class ChoiceField(WritableField):
|
|||
return True
|
||||
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):
|
||||
type_name = 'EmailField'
|
||||
|
|
|
@ -688,6 +688,14 @@ class ChoiceFieldTests(TestCase):
|
|||
f = serializers.ChoiceField(required=False, choices=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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user