aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/serializers.py
diff options
context:
space:
mode:
authorMathieu Pillard2013-11-05 17:21:18 +0100
committerMathieu Pillard2013-11-05 17:21:18 +0100
commit53258908210b1eabd0ee204653a589d6579ac772 (patch)
tree02a9b0d94c71e975bf62774415b494ad8622cc0e /rest_framework/serializers.py
parent003b9dda342e1fbb9b482a3103599e62cc458911 (diff)
downloaddjango-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/serializers.py')
-rw-r--r--rest_framework/serializers.py2
1 files changed, 2 insertions, 0 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index 4210d058..5240dbf6 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -794,6 +794,8 @@ class ModelSerializer(Serializer):
# TODO: TypedChoiceField?
if model_field.flatchoices: # This ModelField contains choices
kwargs['choices'] = model_field.flatchoices
+ if model_field.null:
+ kwargs['empty'] = None
return ChoiceField(**kwargs)
# put this below the ChoiceField because min_value isn't a valid initializer