[Django] #32347: ModelChoiceField does not provide value of invalid choice when raising ValidationError

37 views
Skip to first unread message

Django

unread,
Jan 12, 2021, 11:24:07 PM1/12/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
----------------------------------------+------------------------
Reporter: Aaron Wiegel | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
----------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32347>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 12, 2021, 11:28:58 PM1/12/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
--------------------------+--------------------------------------
Reporter: aawiegel | Owner: nobody

Type: Bug | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------+--------------------------------------
Description changed by aawiegel:

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>

Django

unread,
Jan 13, 2021, 2:06:52 AM1/13/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
------------------------------+----------------------------------------
Reporter: Aaron Wiegel | Owner: Aaron Wiegel
Type: Bug | Status: assigned
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Aaron Wiegel):

* owner: nobody => Aaron Wiegel
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:2>

Django

unread,
Jan 14, 2021, 10:12:10 AM1/14/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
------------------------------+----------------------------------------
Reporter: Aaron Wiegel | Owner: Aaron Wiegel
Type: Bug | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Carlton Gibson):

* 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>

Django

unread,
Jan 14, 2021, 2:19:09 PM1/14/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
------------------------------+----------------------------------------
Reporter: Aaron Wiegel | Owner: Aaron Wiegel
Type: Bug | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------

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>

Django

unread,
Jan 14, 2021, 2:20:34 PM1/14/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
------------------------------+----------------------------------------
Reporter: Aaron Wiegel | Owner: Aaron Wiegel
Type: Bug | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Aaron Wiegel):

* status: closed => new
* resolution: wontfix =>


--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:5>

Django

unread,
Jan 19, 2021, 2:15:42 AM1/19/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
------------------------------+----------------------------------------
Reporter: Aaron Wiegel | Owner: Aaron Wiegel
Type: New feature | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Carlton Gibson):

* 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>

Django

unread,
Jan 25, 2021, 11:03:00 PM1/25/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
-------------------------------------+-------------------------------------
Reporter: Aaron Wiegel | Owner: Jerin
| Peter George
Type: New feature | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jerin Peter George):

* 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>

Django

unread,
Jan 26, 2021, 12:20:05 AM1/26/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
-------------------------------------+-------------------------------------
Reporter: Aaron Wiegel | Owner: Jerin
| Peter George
Type: New feature | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32347#comment:8>

Django

unread,
Jan 26, 2021, 3:35:00 AM1/26/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
-------------------------------------+-------------------------------------
Reporter: Aaron Wiegel | Owner: Jerin
| Peter George
Type: New feature | Status: assigned
Component: Forms | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Jan 26, 2021, 4:57:13 AM1/26/21
to django-...@googlegroups.com
#32347: ModelChoiceField does not provide value of invalid choice when raising
ValidationError
-------------------------------------+-------------------------------------
Reporter: Aaron Wiegel | Owner: Jerin
| Peter George
Type: New feature | Status: closed
Component: Forms | Version: 4.0
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages