Opened 17 years ago

Closed 17 years ago

#5145 closed (invalid)

MultiWidget / MultiValueField does not interpret initial data

Reported by: daofma@… Owned by: Manuel Saelices
Component: Forms Version: dev
Severity: Keywords: newforms, MultiWidget, MultiValueField, initial
Cc: lurker86@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Below I've used SplitDateTime as an example, but it applies to other MultiWidget / MultiValueField as well, as I came across the issue using a custom MultiWidget / MultiValueField. I'm not sure in which the problem lies. Generally, the MultiValueField accepts values from the "data=" argument to the form constructor, and these appear when the MultiWidget is rendered just fine. However, when a dictionary is passed through the "initial=" argument, it does not appear. An example follows:

>>> from django.newforms import *
>>> class TestForm(Form):
...     the_datetime_field = SplitDateTimeField(widget=SplitDateTimeWidget)
... 
>>> test_data = {'the_datetime_field_0':'12/24/07', 'the_datetime_field_1':'15:00'}
>>> unbound_form_no_initial = TestForm()
>>> unbound_form_no_initial.as_p()
u'<p><label for="id_the_datetime_field_0">The datetime field:</label> <input type="text" name="the_datetime_field_0" id="id_the_datetime_field_0" /><input type="text" name="the_datetime_field_1" id="id_the_datetime_field_1" /></p>'
>>> bound_form = TestForm(data=test_data)
>>> bound_form.as_p()
u'<p><label for="id_the_datetime_field_0">The datetime field:</label> <input type="text" name="the_datetime_field_0" value="12/24/07" id="id_the_datetime_field_0" /><input type="text" name="the_datetime_field_1" value="15:00" id="id_the_datetime_field_1" /></p>'
>>> unbound_form_with_initial = TestForm(initial=test_data)
>>> unbound_form_with_initial.as_p()
u'<p><label for="id_the_datetime_field_0">The datetime field:</label> <input type="text" name="the_datetime_field_0" id="id_the_datetime_field_0" /><input type="text" name="the_datetime_field_1" id="id_the_datetime_field_1" /></p>'

As you can see, only the bound form shows the values. The first unbound form is of course not supposed to show any initial values. However, the second unbound form is passed initial values that it is supposed to show, but it does not.

Attachments (1)

docs_newforms_txt.diff (482 bytes ) - added by Nis Jørgensen <nis@…> 17 years ago.
An update to the documentation, since this seems to have bitten a few people.

Download all attachments as: .zip

Change History (6)

comment:1 by lurker86@…, 17 years ago

Cc: lurker86@… added

I met this problem too.
Does anyone have some solutions?

comment:2 by Manuel Saelices, 17 years ago

Owner: changed from nobody to Manuel Saelices
Status: newassigned

comment:3 by Manuel Saelices, 17 years ago

Resolution: invalid
Status: assignedclosed

Is invalid because initial cannot be splitted in two or more values. The correct code was:

>>> from datetime import datetime
>>> from django.newforms import *
>>> class TestForm(Form):
...     the_datetime_field = SplitDateTimeField(widget=SplitDateTimeWidget)
... 
>>> test_data = {'the_datetime_field': datetime(2007, 12, 24, 15, 00)}
>>> unbound_form_with_initial = TestForm(initial=test_data)
>>> unbound_form_with_initial.as_p()
u'<p><label for="id_the_datetime_field_0">The datetime field:</label> <input type="text" name="the_datetime_field_0" value="2007-12-24" id="id_the_datetime_field_0" /><input type="text" name="the_datetime_field_1" value="15:00:00" id="id_the_datetime_field_1" /></p>'

comment:4 by Manuel Saelices, 17 years ago

Needs documentation: set
Resolution: invalid
Status: closedreopened

I update docs for made a clarification about difference between initial and data

by Nis Jørgensen <nis@…>, 17 years ago

Attachment: docs_newforms_txt.diff added

An update to the documentation, since this seems to have bitten a few people.

comment:5 by Manuel Saelices, 17 years ago

Needs documentation: unset
Resolution: invalid
Status: reopenedclosed

I created a more specific ticket #5469 for documenting this.

Note: See TracTickets for help on using tickets.
Back to Top