Opened 5 years ago
Closed 5 years ago
#32347 closed New feature (fixed)
ModelChoiceField does not provide value of invalid choice when raising ValidationError
| Reported by: | Aaron Wiegel | Owned by: | Jerin Peter George |
|---|---|---|---|
| Component: | Forms | Version: | 4.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Compared with ChoiceField and others, ModelChoiceField does not show the value of the invalid choice when raising a validation error. Passing in parameters with the invalid value and modifying the default error message for the code invalid_choice should fix this.
From source code:
class ModelMultipleChoiceField(ModelChoiceField):
"""A MultipleChoiceField whose choices are a model QuerySet."""
widget = SelectMultiple
hidden_widget = MultipleHiddenInput
default_error_messages = {
'invalid_list': _('Enter a list of values.'),
'invalid_choice': _('Select a valid choice. %(value)s is not one of the'
' available choices.'),
'invalid_pk_value': _('“%(pk)s” is not a valid value.')
}
...
class ModelChoiceField(ChoiceField):
"""A ChoiceField whose choices are a model QuerySet."""
# This class is a subclass of ChoiceField for purity, but it doesn't
# actually use any of ChoiceField's implementation.
default_error_messages = {
'invalid_choice': _('Select a valid choice. That choice is not one of'
' the available choices.'),
}
...
Change History (10)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 5 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
follow-up: 4 comment:3 by , 5 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | assigned → closed |
comment:4 by , 5 years ago
Replying to Carlton Gibson:
This message has been the same literally forever b2b6fc8e3c78671c8b6af2709358c3213c84d119.
Given that ChoiceField passes the value when raising the error, if you set error_messages you should be able to get the result you want.
That is ChoiceField. ModelChoiceField (does not pass the value)https://github.com/django/django/blob/a948d9df394aafded78d72b1daa785a0abfeab48/django/forms/models.py#L1287 to the validation error. So, when the invalid value error is raised, you can't display the offending value even if you override the defaults.
comment:5 by , 5 years ago
| Resolution: | wontfix |
|---|---|
| Status: | closed → new |
comment:6 by , 5 years ago
| Easy pickings: | unset |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → New feature |
OK, if you want to look at submitting a PR we can see if any objections come up in review. Thanks.
comment:7 by , 5 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:8 by , 5 years ago
| Needs documentation: | set |
|---|---|
| Patch needs improvement: | set |
comment:9 by , 5 years ago
| Needs documentation: | unset |
|---|---|
| Patch needs improvement: | unset |
| Triage Stage: | Accepted → Ready for checkin |
| Version: | 3.1 → 4.0 |
This message has been the same literally forever b2b6fc8e3c78671c8b6af2709358c3213c84d119.
Given that ChoiceField passes the value when raising the error, if you set error_messages you should be able to get the result you want.