Re: [Django] #3482: MultipleChoiceField validation against a list with a single null element

1 view
Skip to first unread message

Django

unread,
Sep 1, 2021, 1:57:40 PM9/1/21
to django-...@googlegroups.com
#3482: MultipleChoiceField validation against a list with a single null element
-------------------------------------+-------------------------------------
Reporter: Jeff Bauer | Owner: nobody
<jbauer@…> |
Type: Uncategorized | Status: new
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: MultipleChoiceField | 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 Joshua Smith):

* status: closed => new
* severity: => Normal
* resolution: fixed =>
* easy: => 0
* ui_ux: => 0
* type: => Uncategorized


Comment:

This is still happening for me in Django 3.1.13:
{{{
>>> from django.forms import MultipleChoiceField
>>> f = MultipleChoiceField(required=False, choices=[('1', '1'), ('2',
'2')])
>>> f.clean([''])
Traceback (most recent call last):
File "/Users/joshsmith/.pyenv/versions/3.9.4/lib/python3.9/code.py",
line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/Users/joshsmith/code/course-platform/.venv/lib/python3.9/site-
packages/django/forms/fields.py", line 150, in clean
self.validate(value)
File "/Users/joshsmith/code/course-platform/.venv/lib/python3.9/site-
packages/django/forms/fields.py", line 883, in validate
raise ValidationError(
django.core.exceptions.ValidationError: ['Select a valid choice. is not
one of the available choices.']
}}}

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

Django

unread,
Sep 1, 2021, 6:55:30 PM9/1/21
to django-...@googlegroups.com
#3482: MultipleChoiceField validation against a list with a single null element
-------------------------------------+-------------------------------------
Reporter: Jeff Bauer | Owner: nobody
<jbauer@…> |
Type: Bug | Status: new

Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: MultipleChoiceField | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* has_patch: 1 => 0
* type: Uncategorized => Bug
* stage: Ready for checkin => Unreviewed


--
Ticket URL: <https://code.djangoproject.com/ticket/3482#comment:6>

Django

unread,
Sep 2, 2021, 4:13:43 AM9/2/21
to django-...@googlegroups.com
#3482: MultipleChoiceField validation against a list with a single null element
-------------------------------------+-------------------------------------
Reporter: Jeff Bauer | Owner: nobody
<jbauer@…> |
Type: Bug | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: wontfix

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

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


Comment:

So, it's hard to say about the dark-history here, as bootstrapping a very
old environment is somewhat fiddly these days but
[https://github.com/django/django/blob/d0ea01af2814e276c4a090e599d5fd11c6ce4bf8/django/forms/fields.py#L884-L890
the logic here] is virtually unchanged from the
[https://github.com/django/django/commit/6645d1fe48868814e4c73056b68be5c3861ed2d0
#diff-
19fae1b4e3786d4af8cdf8db08d18d687caeae43859b35c769deb96d3aacf05cR256-R260
earliest days of the MultipleChoiceField implementation]. It's certainly
unchanged as far back as Django 1.8. Without a serious case being made,
the backwards compatibility concerns preclude changing this now.

However, for me, this is expected behaviour: `''` is **not** a valid
choice. If I want filtering of empty values then I'm expecting to use a
widget to filter this in `value_from_datadict`, or else a field subclass,
if I want the behaviour there. The default implementation is doing the
right thing here rejecting the incorrect value.

--
Ticket URL: <https://code.djangoproject.com/ticket/3482#comment:7>

Reply all
Reply to author
Forward
0 new messages