diff options
| author | Mathieu Pillard | 2013-11-05 17:21:18 +0100 |
|---|---|---|
| committer | Mathieu Pillard | 2013-11-05 17:21:18 +0100 |
| commit | 53258908210b1eabd0ee204653a589d6579ac772 (patch) | |
| tree | 02a9b0d94c71e975bf62774415b494ad8622cc0e /rest_framework/fields.py | |
| parent | 003b9dda342e1fbb9b482a3103599e62cc458911 (diff) | |
| download | django-rest-framework-53258908210b1eabd0ee204653a589d6579ac772.tar.bz2 | |
Improve handling of 'empty' values for ChoiceField
The empty value defaults back to '' (for backwards-compatibility) but
is changed automatically to None for ModelSerializers if the `null`
property is set on the db field.
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e23fc001..6c07dbb3 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -497,6 +497,7 @@ class ChoiceField(WritableField): } def __init__(self, choices=(), *args, **kwargs): + self.empty = kwargs.pop('empty', '') super(ChoiceField, self).__init__(*args, **kwargs) self.choices = choices if not self.required: @@ -537,9 +538,10 @@ class ChoiceField(WritableField): return False def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None - return super(ChoiceField, self).from_native(value) + value = super(ChoiceField, self).from_native(value) + if value == self.empty or value in validators.EMPTY_VALUES: + return self.empty + return value class EmailField(CharField): |
