Skip to content

Commit

Permalink
Merge pull request #2940 from rapilabs/master
Browse files Browse the repository at this point in the history
Allow unexpected values for ChoiceField/MultipleChoiceField representations
  • Loading branch information
tomchristie committed May 15, 2015
2 parents 6add1ac + b7edd46 commit e33fed7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ def to_internal_value(self, data):
def to_representation(self, value):
if value in ('', None):
return value
return self.choice_strings_to_values[six.text_type(value)]
return self.choice_strings_to_values.get(six.text_type(value), value)


class MultipleChoiceField(ChoiceField):
Expand Down Expand Up @@ -1073,7 +1073,7 @@ def to_internal_value(self, data):

def to_representation(self, value):
return set([
self.choice_strings_to_values[six.text_type(item)] for item in value
self.choice_strings_to_values.get(six.text_type(item), item) for item in value
])


Expand Down
5 changes: 3 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ class TestChoiceField(FieldValues):
}
outputs = {
'good': 'good',
'': ''
'': '',
'amazing': 'amazing',
}
field = serializers.ChoiceField(
choices=[
Expand Down Expand Up @@ -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=[
Expand Down

0 comments on commit e33fed7

Please sign in to comment.