aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
authorJosé Padilla2014-12-09 09:25:06 -0400
committerJosé Padilla2014-12-09 09:25:06 -0400
commitafe7ed9333e37384f8ddc57e891da9632c8714c3 (patch)
treec45f1a1242b9c12f4e1e3aaaab2ff6eb90473ed9 /rest_framework/fields.py
parent0f080bc2932095bc6dd2c71f545f2b588985431d (diff)
downloaddjango-rest-framework-afe7ed9333e37384f8ddc57e891da9632c8714c3.tar.bz2
Add allow_blank for ChoiceField #2184
This makes a ChoiceField optional in HTML if model field has `blank=True` set.
Diffstat (limited to 'rest_framework/fields.py')
-rw-r--r--rest_framework/fields.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 0c6c2d39..99498da7 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -958,9 +958,14 @@ class ChoiceField(Field):
(six.text_type(key), key) for key in self.choices.keys()
])
+ self.allow_blank = kwargs.pop('allow_blank', False)
+
super(ChoiceField, self).__init__(**kwargs)
def to_internal_value(self, data):
+ if data == '' and self.allow_blank:
+ return ''
+
try:
return self.choice_strings_to_values[six.text_type(data)]
except KeyError: