Skip to content

Commit b7edd46

Browse files
author
David Sanders
committed
Use simpler dict.get() rather than try/except
1 parent 8e84a9f commit b7edd46

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

rest_framework/fields.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,16 +1042,10 @@ def to_internal_value(self, data):
10421042
except KeyError:
10431043
self.fail('invalid_choice', input=data)
10441044

1045-
def representation_value(self, value):
1046-
try:
1047-
return self.choice_strings_to_values[six.text_type(value)]
1048-
except KeyError:
1049-
return value
1050-
10511045
def to_representation(self, value):
10521046
if value in ('', None):
10531047
return value
1054-
return self.representation_value(value)
1048+
return self.choice_strings_to_values.get(six.text_type(value), value)
10551049

10561050

10571051
class MultipleChoiceField(ChoiceField):
@@ -1079,7 +1073,7 @@ def to_internal_value(self, data):
10791073

10801074
def to_representation(self, value):
10811075
return set([
1082-
self.representation_value(item) for item in value
1076+
self.choice_strings_to_values.get(six.text_type(item), item) for item in value
10831077
])
10841078

10851079

0 commit comments

Comments
 (0)