mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-26 12:03:44 +03:00
Allow invalid outputs to pass through to_representation() for ChoiceField & MultipleChoiceField
This commit is contained in:
parent
6add1acc4e
commit
8e84a9f6d0
|
@ -1042,10 +1042,16 @@ class ChoiceField(Field):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.fail('invalid_choice', input=data)
|
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):
|
def to_representation(self, value):
|
||||||
if value in ('', None):
|
if value in ('', None):
|
||||||
return value
|
return value
|
||||||
return self.choice_strings_to_values[six.text_type(value)]
|
return self.representation_value(value)
|
||||||
|
|
||||||
|
|
||||||
class MultipleChoiceField(ChoiceField):
|
class MultipleChoiceField(ChoiceField):
|
||||||
|
@ -1073,7 +1079,7 @@ class MultipleChoiceField(ChoiceField):
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
return set([
|
return set([
|
||||||
self.choice_strings_to_values[six.text_type(item)] for item in value
|
self.representation_value(item) for item in value
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -920,7 +920,8 @@ class TestChoiceField(FieldValues):
|
||||||
}
|
}
|
||||||
outputs = {
|
outputs = {
|
||||||
'good': 'good',
|
'good': 'good',
|
||||||
'': ''
|
'': '',
|
||||||
|
'amazing': 'amazing',
|
||||||
}
|
}
|
||||||
field = serializers.ChoiceField(
|
field = serializers.ChoiceField(
|
||||||
choices=[
|
choices=[
|
||||||
|
@ -1005,7 +1006,7 @@ class TestMultipleChoiceField(FieldValues):
|
||||||
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
|
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
|
||||||
}
|
}
|
||||||
outputs = [
|
outputs = [
|
||||||
(['aircon', 'manual'], set(['aircon', 'manual']))
|
(['aircon', 'manual', 'incorrect'], set(['aircon', 'manual', 'incorrect']))
|
||||||
]
|
]
|
||||||
field = serializers.MultipleChoiceField(
|
field = serializers.MultipleChoiceField(
|
||||||
choices=[
|
choices=[
|
||||||
|
|
Loading…
Reference in New Issue
Block a user