Skip to content

Commit e33fed7

Browse files
committed
Merge pull request #2940 from rapilabs/master
Allow unexpected values for ChoiceField/MultipleChoiceField representations
2 parents 6add1ac + b7edd46 commit e33fed7

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

rest_framework/fields.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def to_internal_value(self, data):
10451045
def to_representation(self, value):
10461046
if value in ('', None):
10471047
return value
1048-
return self.choice_strings_to_values[six.text_type(value)]
1048+
return self.choice_strings_to_values.get(six.text_type(value), value)
10491049

10501050

10511051
class MultipleChoiceField(ChoiceField):
@@ -1073,7 +1073,7 @@ def to_internal_value(self, data):
10731073

10741074
def to_representation(self, value):
10751075
return set([
1076-
self.choice_strings_to_values[six.text_type(item)] for item in value
1076+
self.choice_strings_to_values.get(six.text_type(item), item) for item in value
10771077
])
10781078

10791079

tests/test_fields.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ class TestChoiceField(FieldValues):
920920
}
921921
outputs = {
922922
'good': 'good',
923-
'': ''
923+
'': '',
924+
'amazing': 'amazing',
924925
}
925926
field = serializers.ChoiceField(
926927
choices=[
@@ -1005,7 +1006,7 @@ class TestMultipleChoiceField(FieldValues):
10051006
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
10061007
}
10071008
outputs = [
1008-
(['aircon', 'manual'], set(['aircon', 'manual']))
1009+
(['aircon', 'manual', 'incorrect'], set(['aircon', 'manual', 'incorrect']))
10091010
]
10101011
field = serializers.MultipleChoiceField(
10111012
choices=[

0 commit comments

Comments
 (0)