From e49d22dbda5ac889ab89f277e17752c840819de2 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Nov 2014 09:31:26 +0000 Subject: [PATCH] Allow blank choices to render. Closes #2071. --- rest_framework/fields.py | 2 ++ tests/test_fields.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 36afe7a99..bb43708dc 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -947,6 +947,8 @@ class ChoiceField(Field): self.fail('invalid_choice', input=data) def to_representation(self, value): + if value in ('', None): + return value return self.choice_strings_to_values[six.text_type(value)] diff --git a/tests/test_fields.py b/tests/test_fields.py index 5db381acc..135256320 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -793,7 +793,8 @@ class TestChoiceField(FieldValues): 'amazing': ['`amazing` is not a valid choice.'] } outputs = { - 'good': 'good' + 'good': 'good', + '': '' } field = serializers.ChoiceField( choices=[