Opened 8 years ago

Closed 8 years ago

#5145 closed (invalid)

MultiWidget / MultiValueField does not interpret initial data

Reported by: daofma@… Owned by: msaelices
Component: Forms Version: master
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: UI/UX:

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@…> 8 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 Changed 8 years ago by lurker86@…

  • Cc lurker86@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 8 years ago by msaelices

  • Owner changed from nobody to msaelices
  • Status changed from new to assigned

comment:3 Changed 8 years ago by msaelices

  • Resolution set to invalid
  • Status changed from assigned to 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 Changed 8 years ago by msaelices

  • Needs documentation set
  • Resolution invalid deleted
  • Status changed from closed to reopened

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

Changed 8 years ago by Nis Jørgensen <nis@…>

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

comment:5 Changed 8 years ago by msaelices

  • Needs documentation unset
  • Resolution set to invalid
  • Status changed from reopened to closed

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

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