-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow unexpected values for ChoiceField
/MultipleChoiceField
representations
#2940
Conversation
…ield & MultipleChoiceField
try: | ||
return self.choice_strings_to_values[six.text_type(value)] | ||
except KeyError: | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably prefer to drop the indirection & just include this logic in the code as-is without the extra method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was actually my first attempt, although how would you inline a try/except inside a list comprehension? Was asking on #django and was told it may be possible with a generator expression & yielding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yup, I see that now. Missed the wrapping list comprehension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you just do if six.text_type(value) in self.choice_strings_to_values
instead of relying on a try/except here?
(Not suggesting that we remove the extra method, just commenting on the try/except that is being used)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, perhaps a self.choice_strings_to_values.get(six.text_type(value), value)
is better here?
@tomchristie an inline attempt: https://gist.github.com/rapilabs/93b983ea2313aa71137a |
Method call is simpler afterall :) |
Allow unexpected values for ChoiceField/MultipleChoiceField representations
Perfect! |
ChoiceField
/MultipleChoiceField
representations
Closes #2839