[Django] #27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField

42 views
Skip to first unread message

Django

unread,
Mar 29, 2017, 3:34:34 AM3/29/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: olivierd | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords: ArrayField
Triage Stage: | postgresql MultipleChoiceField
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
While using a {{{forms.MultipleChoiceField}}} to represent an
{{{ArrayField}}}, there is no way to clear the {{{ArrayField}}}.
Unselecting all items from the rendred {{{<select mutiple="multiple">}}}
correctly leads to an empty list showing up in the form's
{{{cleaned_data}}} but the value is ignored. The problem is that
{{{django.forms.models.construct_instance}}} ignores the value due to
{{{django.forms.widgets.SelectMultiple}}} (correctly) returning {{{True}}}
from {{{value_omitted_from_data(...)}}}.

The following small code example will demonstrate the issue by failing on
the last assert.
{{{
from django.db import models
from django import forms
from django.contrib.postgres.fields import ArrayField

class TestModel(models.Model):
array = ArrayField(models.CharField(max_length=10), default=list,
blank=True)
class Meta:
app_label = 'test'

class TestForm(forms.ModelForm):
array = forms.MultipleChoiceField(choices=(('one', 'One'), ('two',
'Two')), required=False)
class Meta:
model = TestModel
fields = '__all__'

test_instance = TestModel(array=['one'])
assert test_instance.array == ['one']
test_form = TestForm(instance=test_instance)
test_form = TestForm(data=[], instance=test_instance)
assert test_form.is_valid()
assert test_form.cleaned_data == {'array': []}
updated_instance = test_form.save(commit=False)
assert updated_instance.array == []
}}}

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

Django

unread,
Mar 29, 2017, 9:18:12 AM3/29/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: nobody

Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: ArrayField | Triage Stage: Accepted
postgresql MultipleChoiceField |

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

* stage: Unreviewed => Accepted


Comment:

The fix is to add `SelectMultiple.value_omitted_from_data()` that always
returns `False`, similar to 87c5e7efebd040aef0f0479ccf86877155bb5cea.

--
Ticket URL: <https://code.djangoproject.com/ticket/27993#comment:1>

Django

unread,
Mar 29, 2017, 10:20:04 PM3/29/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: heathervm
Type: Bug | Status: assigned
Component: Forms | Version: 1.10

Severity: Normal | Resolution:
Keywords: ArrayField | Triage Stage: Accepted
postgresql MultipleChoiceField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by heathervm):

* status: new => assigned
* owner: nobody => heathervm


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

Django

unread,
Mar 30, 2017, 8:50:00 AM3/30/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: heathervm
Type: Bug | Status: assigned
Component: Forms | Version: 1.10

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

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

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/8266 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27993#comment:3>

Django

unread,
Mar 31, 2017, 10:10:21 AM3/31/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: heathervm
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: fixed

Keywords: ArrayField | Triage Stage: Accepted
postgresql MultipleChoiceField |
Has patch: 1 | Needs documentation: 0

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

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7d1e23775344cc3dead03bd4af45f4fdf134b819" 7d1e237]:
{{{
#!CommitTicketReference repository=""
revision="7d1e23775344cc3dead03bd4af45f4fdf134b819"
Fixed #27993 -- Fixed model form default fallback for SelectMultiple.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27993#comment:4>

Django

unread,
Mar 31, 2017, 10:16:07 AM3/31/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: heathervm
Type: Bug | Status: closed
Component: Forms | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: ArrayField | Triage Stage: Accepted
postgresql MultipleChoiceField |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"ff0c6b83e590e36555639563c4bd1d808f416455" ff0c6b83]:
{{{
#!CommitTicketReference repository=""
revision="ff0c6b83e590e36555639563c4bd1d808f416455"
[1.11.x] Fixed #27993 -- Fixed model form default fallback for
SelectMultiple.

Backport of 7d1e23775344cc3dead03bd4af45f4fdf134b819 from master
}}}

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

Django

unread,
Mar 31, 2017, 10:26:39 AM3/31/17
to django-...@googlegroups.com
#27993: Impossible to clear an ArrayField with a forms.MultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Olivier Dormond | Owner: heathervm
Type: Bug | Status: closed
Component: Forms | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: ArrayField | Triage Stage: Accepted
postgresql MultipleChoiceField |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"83331d1ed0fde144767e8f35f2d9e512bdd2afb1" 83331d1]:
{{{
#!CommitTicketReference repository=""
revision="83331d1ed0fde144767e8f35f2d9e512bdd2afb1"
[1.10.x] Fixed #27993 -- Fixed model form default fallback for
SelectMultiple.

Backport of 7d1e23775344cc3dead03bd4af45f4fdf134b819 from master
}}}

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

Reply all
Reply to author
Forward
0 new messages