Opened 17 years ago
Closed 17 years ago
#5145 closed (invalid)
MultiWidget / MultiValueField does not interpret initial data
Reported by: | 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)
Change History (6)
comment:1 by , 17 years ago
Cc: | added |
---|
comment:2 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
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 , 17 years ago
Needs documentation: | set |
---|---|
Resolution: | invalid |
Status: | closed → reopened |
I update docs for made a clarification about difference between initial
and data
by , 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 , 17 years ago
Needs documentation: | unset |
---|---|
Resolution: | → invalid |
Status: | reopened → closed |
I created a more specific ticket #5469 for documenting this.
I met this problem too.
Does anyone have some solutions?