diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 0c6c2d390d..99498da73a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -958,9 +958,14 @@ def __init__(self, choices, **kwargs): (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: diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index fb6c826b8b..b0c0efa7c8 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -942,7 +942,7 @@ def get_fields(self): # `ModelField`, which is used when no other typed field # matched to the model field. kwargs.pop('model_field', None) - if not issubclass(field_cls, CharField): + if not issubclass(field_cls, CharField) and not issubclass(field_cls, ChoiceField): # `allow_blank` is only valid for textual fields. kwargs.pop('allow_blank', None) diff --git a/rest_framework/templates/rest_framework/horizontal/select.html b/rest_framework/templates/rest_framework/horizontal/select.html index 380b38e94d..8a7fca370f 100644 --- a/rest_framework/templates/rest_framework/horizontal/select.html +++ b/rest_framework/templates/rest_framework/horizontal/select.html @@ -4,7 +4,7 @@ {% endif %}
- {% if field.allow_null %} + {% if field.allow_null or field.allow_blank %} {% endif %} {% for key, text in field.choices.items %} diff --git a/rest_framework/templates/rest_framework/vertical/select.html b/rest_framework/templates/rest_framework/vertical/select.html index de72e1ddbf..1d1109f6ea 100644 --- a/rest_framework/templates/rest_framework/vertical/select.html +++ b/rest_framework/templates/rest_framework/vertical/select.html @@ -3,7 +3,7 @@ {% endif %}