[Django] #28531: MultipleChoiceField / CheckboxSelectMultiple values not restored on bound forms

31 views
Skip to first unread message

Django

unread,
Aug 25, 2017, 7:39:48 AM8/25/17
to django-...@googlegroups.com
#28531: MultipleChoiceField / CheckboxSelectMultiple values not restored on bound
forms
--------------------------------------+------------------------
Reporter: Benjamin | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
I'm building the 'edit' page of my form. This page is supposed to show
form with the data that was saved, so the form is pre populated with saved
data. It works fine for most if the fields, but I have a problem with
'MultipleChoiceField' / 'CheckboxSelectMultiple' values that don't get
restored. So instead of having the corresponding checkboxes checked with
data from the saved form, they are all unchecked. This also affects
'SelectMultiple'
Could this be a bug ?


{{{
# forms.py
class MemberForm( forms.ModelForm ):

# ......

MODEL_CATEGORIES = (
('advisor', 'advisor'),
('member', 'member'),
('admin', 'admin'),
)


model_categories = forms.MultipleChoiceField(
widget = forms.CheckboxSelectMultiple,
choices = MODEL_CATEGORIES
)

class Meta:
model = Member
fields = [ 'model_categories' ]


# model
class Member( models.Model ):

model_categories = models.CharField(
max_length = 255,
null = True,
blank = True )

# Controller
def profile_edit_form( request ):
user = request.user or None

# Get user member profile instance
instance = get_object_or_404( Member, author = user )

form = MemberForm( request.POST or None, instance = instance )

context = {
"form" : form,
"instance": instance
}

if form.is_valid():
# ...
return redirect( 'profile_display' )
else:
# Initial form display, and redisplay of invalid form
return render( request, 'profile_edit_form_view.html', context )

# Template
<form action="/accounts/profile-edit-form/" method="post">
{% csrf_token %}

{{ form }}


<input type="submit" value="Submit"/>
</form>
}}}

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

Django

unread,
Aug 25, 2017, 8:54:51 AM8/25/17
to django-...@googlegroups.com
#28531: MultipleChoiceField / CheckboxSelectMultiple values not restored on bound
forms
--------------------------+--------------------------------------

Reporter: Benjamin | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.11
Severity: Normal | Resolution:

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

Comment (by Benjamin):

Remark: this is probably because multiple values stored in database field
as retrieved as

{{{
'[\\'advisor\\', \\'member\\']'
}}}

which the bound form cannot understand when it restores values.

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

Django

unread,
Aug 25, 2017, 12:57:04 PM8/25/17
to django-...@googlegroups.com
#28531: MultipleChoiceField / CheckboxSelectMultiple values linked to a CharField
not restored on bound model forms
--------------------------+--------------------------------------
Reporter: Benjamin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: duplicate

Keywords: | 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):

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


Comment:

Looks like a duplicate of #27495. As far as I know, using those widgets
with `CharField` isn't supported (and shouldn't be, in my opinion). It's
better practice to store choices in a many to many table than in a
`CharField`.

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

Django

unread,
Aug 26, 2017, 9:42:52 AM8/26/17
to django-...@googlegroups.com
#28531: MultipleChoiceField / CheckboxSelectMultiple values linked to a CharField
not restored on bound model forms
--------------------------+--------------------------------------
Reporter: Benjamin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: duplicate

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

Comment (by Benjamin):

Replying to [comment:2 Tim Graham]:


> Looks like a duplicate of #27495. As far as I know, using those widgets
with `CharField` isn't supported (and shouldn't be, in my opinion). It's
better practice to store choices in a many to many table than in a
`CharField`.

It works fine with postgres array field: values are properly stored and
restored.

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

Reply all
Reply to author
Forward
0 new messages