#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.385d6fd7815d6dace186a00374f311f6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to