From afe7ed9333e37384f8ddc57e891da9632c8714c3 Mon Sep 17 00:00:00 2001 From: José Padilla Date: Tue, 9 Dec 2014 09:25:06 -0400 Subject: Add allow_blank for ChoiceField #2184 This makes a ChoiceField optional in HTML if model field has `blank=True` set.--- rest_framework/fields.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'rest_framework/fields.py') 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: -- cgit v1.2.3