-
-
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.
#2839
Comments
Totally agree! |
Additionally, the current behaviour does not allow to have one set of fields for the output and another for input. Eg. we have a 3 different values possible on the model layer, but I only want to accept 2 of them in the input of a specific serializer (yet return all 3 of them in the output if they're there). Solvable with a custom field, but still a pain. |
Happy to accept an issue for this if it's presented as an example with:
As far as the remainder of the ticket - it's unclear to me what's being suggested, but it sounds like a case for a custom field, as does @maryokhin's different input and output requirement. |
The key of the current behaviour is difference from Django ORM behaviour. |
Ah, misunderstood the ticket here I think - so the request is that unexpected values should be passed through as-is to the representation. Sure I've no great problem with that. Happy to accept a pull request modifying the behavior there, and including an example test case. |
Exactly! |
ChoiceField
/MultipleChoiceField
representations.
When an invalid value is present in the database that isn't part of the list of choices (perhaps dealing with a legacy database) a KeyError will be raised in to_representation() of ChoiceField/MultipleChoiceField.
Personally I'd like to see the behaviour more in line with the Django ORM's choices behaviour where the invalid value is still readable, perhaps by simply returning it from to_representation()
In case this is disagreeable, I think the KeyError should be caught and a new exception raised with a more easily debuggable error message explaining which field has the invalid value.
The text was updated successfully, but these errors were encountered: