[Django] #18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work correctly

42 views
Skip to first unread message

Django

unread,
Aug 3, 2012, 12:11:55 PM8/3/12
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
----------------------------+--------------------
Reporter: dekkers | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
The following code doesn't work (it's included as a test case in the
patch):

{{{
#!python
class SplitDateTimeForm(forms.Form):
when = forms.SplitDateTimeField(initial=datetime.datetime.now)

SplitDateTimeFormSet = forms.formsets.formset_factory(SplitDateTimeForm)

data = {
'form-TOTAL_FORMS': '1',
'form-INITIAL_FORMS': '0',
'form-0-when_0': '1904-06-16',
'form-0-when_1': '15:51:33',
}
formset = SplitDateTimeFormSet(data)
formset.is_valid()
}}}

It gives the following stack trace:

{{{
#!python
Traceback (most recent call last):
File
"/home/jeroen/github/django/tests/regressiontests/forms/tests/formsets.py",
line 870, in test_formset_splitdatetimefield
self.assertTrue(formset.is_valid())
File "/home/jeroen/github/django/django/forms/formsets.py", line 271, in
is_valid
err = self.errors
File "/home/jeroen/github/django/django/forms/formsets.py", line 249, in
_get_errors
self.full_clean()
File "/home/jeroen/github/django/django/forms/formsets.py", line 292, in
full_clean
self._errors.append(form.errors)
File "/home/jeroen/github/django/django/forms/forms.py", line 116, in
_get_errors
self.full_clean()
File "/home/jeroen/github/django/django/forms/forms.py", line 269, in
full_clean
if self.empty_permitted and not self.has_changed():
File "/home/jeroen/github/django/django/forms/forms.py", line 324, in
has_changed
return bool(self.changed_data)
File "/home/jeroen/github/django/django/forms/forms.py", line 347, in
_get_changed_data
if field.widget._has_changed(initial_value, data_value):
File "/home/jeroen/github/django/django/forms/widgets.py", line 847, in
_has_changed
initial = self.decompress(initial)
File "/home/jeroen/github/django/django/forms/widgets.py", line 897, in
decompress
return [value.date(), value.time().replace(microsecond=0)]
AttributeError: 'builtin_function_or_method' object has no attribute
'date'
}}}

The problem is that _get_changed_data doesn't check whether initial is a
callable and calls the _has_changed widget method with the callable as
initial_value. Because the default _has_changed does a force_unicode
before comparing, we never get an error for the simple widgets as
force_unicode simply returns
u'<built-in method now of type object at 0x7f77b3be7c40>', so it just
always returns True for such fields. Any widget that does something more
with initial_value will fail however.

The patch fixes _get_changed_data to call the initial value when it is a
callable.

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

Django

unread,
Aug 3, 2012, 12:34:00 PM8/3/12
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+--------------------------------------
Reporter: dekkers | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+--------------------------------------
Changes (by dekkers):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

https://github.com/django/django/pull/246

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

Django

unread,
Sep 7, 2012, 11:18:17 AM9/7/12
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+--------------------------------------
Reporter: dekkers | Owner: jwmayfield
Type: Bug | Status: assigned
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+--------------------------------------
Changes (by jwmayfield):

* cc: jason@… (added)
* owner: nobody => jwmayfield
* status: new => assigned
* stage: Unreviewed => Accepted


Comment:

Patch applies cleanly. Test fails before applying patch. Test passes
after patch.

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

Django

unread,
Sep 7, 2012, 11:37:12 AM9/7/12
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+---------------------------------------------
Reporter: dekkers | Owner: jwmayfield
Type: Bug | Status: assigned
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+---------------------------------------------
Changes (by jwmayfield):

* stage: Accepted => Ready for checkin


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

Django

unread,
Sep 7, 2012, 11:44:30 AM9/7/12
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+---------------------------------------------
Reporter: dekkers | Owner:
Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+---------------------------------------------
Changes (by jwmayfield):

* owner: jwmayfield =>
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:4>

Django

unread,
May 18, 2013, 11:30:00 AM5/18/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+---------------------------------------------
Reporter: dekkers | Owner:

Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+---------------------------------------------

Comment (by galuszkak):

It will be very nice to merge because it is breaking pull request for
ticket #19019

--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:5>

Django

unread,
May 18, 2013, 11:46:30 AM5/18/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+---------------------------------------------
Reporter: dekkers | Owner:

Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+---------------------------------------------

Comment (by aaugustin):

The patch looks good, but unfortunately it doesn't apply cleanly any
longer.

If you bring it up to date, you can mark it as ready for checkin.

--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:6>

Django

unread,
May 18, 2013, 11:46:35 AM5/18/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+------------------------------------
Reporter: dekkers | Owner:

Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+------------------------------------
Changes (by aaugustin):

* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:7>

Django

unread,
May 21, 2013, 7:12:09 PM5/21/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------+---------------------------------------------
Reporter: dekkers | Owner:

Type: Bug | Status: new
Component: Forms | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+---------------------------------------------
Changes (by dekkers):

* stage: Accepted => Ready for checkin


Comment:

I've rebased the commit so it applies again.

--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:8>

Django

unread,
May 22, 2013, 3:45:16 AM5/22/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------------------+-------------------------------------
Reporter: dekkers | Owner: Jeroen
Type: Bug | Dekkers <jeroen@…>
Component: Forms | Status: closed
Severity: Normal | Version: 1.4
Keywords: | Resolution: fixed
Has patch: 1 | Triage Stage: Ready for
Needs tests: 0 | checkin
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jeroen Dekkers <jeroen@…>):

* owner: => Jeroen Dekkers <jeroen@…>
* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"d0788c277035727b7b070abd0f02d075acffc84f"]:
{{{
#!CommitTicketReference repository=""
revision="d0788c277035727b7b070abd0f02d075acffc84f"
Fixed #18709 -- Check if initial_value is a callable

In _get_changed_data, check if initial_value is a callable and call it
if it is.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:9>

Django

unread,
May 22, 2013, 3:45:16 AM5/22/13
to django-...@googlegroups.com
#18709: Formset with SplitDateTimeField(initial=datetime.datetime.now) doesn't work
correctly
-------------------------------------+-------------------------------------
Reporter: dekkers | Owner: Jeroen
Type: Bug | Dekkers <jeroen@…>
Component: Forms | Status: closed
Severity: Normal | Version: 1.4
Keywords: | Resolution: fixed
Has patch: 1 | Triage Stage: Ready for
Needs tests: 0 | checkin
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Marc Tamlyn <marc.tamlyn@…>):

In [changeset:"adeec00979d1b365535b8f26fda4a5f7173e975d"]:
{{{
#!CommitTicketReference repository=""
revision="adeec00979d1b365535b8f26fda4a5f7173e975d"
Merge pull request #246 from dekkers/ticket_18709

Fixed #18709 -- Check if initial_value is a callable
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18709#comment:10>

Reply all
Reply to author
Forward
0 new messages