From 8e84a9f6d0c95eafce29d6ede153db43f143c997 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Fri, 15 May 2015 18:59:10 +1000 Subject: [PATCH 1/2] Allow invalid outputs to pass through to_representation() for ChoiceField & MultipleChoiceField --- rest_framework/fields.py | 10 ++++++++-- tests/test_fields.py | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 460645796..65e7bda65 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1042,10 +1042,16 @@ class ChoiceField(Field): except KeyError: self.fail('invalid_choice', input=data) + def representation_value(self, value): + try: + return self.choice_strings_to_values[six.text_type(value)] + except KeyError: + return value + def to_representation(self, value): if value in ('', None): return value - return self.choice_strings_to_values[six.text_type(value)] + return self.representation_value(value) class MultipleChoiceField(ChoiceField): @@ -1073,7 +1079,7 @@ class MultipleChoiceField(ChoiceField): def to_representation(self, value): return set([ - self.choice_strings_to_values[six.text_type(item)] for item in value + self.representation_value(item) for item in value ]) diff --git a/tests/test_fields.py b/tests/test_fields.py index 1531fe2ac..55e9aacc1 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -920,7 +920,8 @@ class TestChoiceField(FieldValues): } outputs = { 'good': 'good', - '': '' + '': '', + 'amazing': 'amazing', } field = serializers.ChoiceField( choices=[ @@ -1005,7 +1006,7 @@ class TestMultipleChoiceField(FieldValues): ('aircon', 'incorrect'): ['"incorrect" is not a valid choice.'] } outputs = [ - (['aircon', 'manual'], set(['aircon', 'manual'])) + (['aircon', 'manual', 'incorrect'], set(['aircon', 'manual', 'incorrect'])) ] field = serializers.MultipleChoiceField( choices=[ From b7edd463139a02ccad268bc6c9f8a17d02641421 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Sat, 16 May 2015 01:01:27 +1000 Subject: [PATCH 2/2] Use simpler dict.get() rather than try/except --- rest_framework/fields.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 65e7bda65..96fcc257a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1042,16 +1042,10 @@ class ChoiceField(Field): except KeyError: self.fail('invalid_choice', input=data) - def representation_value(self, value): - try: - return self.choice_strings_to_values[six.text_type(value)] - except KeyError: - return value - def to_representation(self, value): if value in ('', None): return value - return self.representation_value(value) + return self.choice_strings_to_values.get(six.text_type(value), value) class MultipleChoiceField(ChoiceField): @@ -1079,7 +1073,7 @@ class MultipleChoiceField(ChoiceField): def to_representation(self, value): return set([ - self.representation_value(item) for item in value + self.choice_strings_to_values.get(six.text_type(item), item) for item in value ])