--
Ticket URL: <https://code.djangoproject.com/ticket/32347>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> 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.
New description:
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.'),
}
...
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:1>
* owner: nobody => Aaron Wiegel
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:2>
* status: assigned => closed
* resolution: => wontfix
Comment:
This message has been the same literally forever
b2b6fc8e3c78671c8b6af2709358c3213c84d119.
[https://github.com/django/django/blob/75182a800a621b7a5b2c0a1f39a56e753b9a58ca/django/forms/fields.py#L802-L810
Given that ChoiceField passes the value when raising the error], if you
set [https://docs.djangoproject.com/en/3.1/ref/forms/fields/#error-
messages error_messages] you should be able to get the result you want.
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:3>
Comment (by Aaron Wiegel):
Replying to [comment:3 Carlton Gibson]:
> This message has been the same literally forever
b2b6fc8e3c78671c8b6af2709358c3213c84d119.
>
>
[https://github.com/django/django/blob/75182a800a621b7a5b2c0a1f39a56e753b9a58ca/django/forms/fields.py#L802-L810
Given that ChoiceField passes the value when raising the error], if you
set [https://docs.djangoproject.com/en/3.1/ref/forms/fields/#error-
messages 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:4>
* status: closed => new
* resolution: wontfix =>
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:5>
* type: Bug => New feature
* easy: 1 => 0
* stage: Unreviewed => Accepted
Comment:
OK, if you want to look at submitting a PR we can see if any objections
come up in review. Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:6>
* owner: Aaron Wiegel => Jerin Peter George
* status: new => assigned
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/13933
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:7>
* needs_better_patch: 0 => 1
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:8>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
* version: 3.1 => 4.0
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"1adc09064f88b69b3b565b57502f2a0a32b5f3a8" 1adc0906]:
{{{
#!CommitTicketReference repository=""
revision="1adc09064f88b69b3b565b57502f2a0a32b5f3a8"
Fixed #32347 -- Made ModelChoiceField include the value in ValidationError
for invalid_choice.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:10>