Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#4317 closed (duplicate)

Fix compress() in SplitDateTimeField

Reported by: ctdecci@… Owned by:
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


If a SplitDateTimeField is not required, and one or both of the date/time fields is None, then datetime.datetime.combine() raises an exception within the compress() method. The current test "if data_list:" (shown below) will only fail if passed an empty list/tuple. If data_list is a non-empty list containing a None value (because one or both of the date/time fields are blank), then the "if data_list:" test still passes, and datetime.datetime.combine() raises an exception.

Snippet from compress() method of SplitDateTimeField class:

if data_list:
    return datetime.datetime.combine(*data_list)

Given this model:

from django.db import models

class Foo(models.Model):
    date = models.DateTimeField(blank=True, null=True)

Here is an example:

>>> from scinet.test.models import Foo
>>> from django import newforms as forms
>>> Form = forms.form_for_model(Foo)
>>> Form.base_fields['date'] = forms.fields.SplitDateTimeField(required=False, widget=forms.widgets.SplitDateTimeWidget)
>>> f = Form({'date_0': None, 'date_1': None})
>>> f.errors
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/newforms/", line 93, in _errors
  File "/usr/lib/python2.4/site-packages/django/newforms/", line 187, in full_clean
    value = field.clean(value)
  File "/usr/lib/python2.4/site-packages/django/newforms/", line 473, in clean
    return self.compress(clean_data)
  File "/usr/lib/python2.4/site-packages/django/newforms/", line 493, in compress
    return datetime.datetime.combine(*data_list)
TypeError: combine() argument 1 must be, not None

I've attached a patch that checks data_list for empty values, verifies that (in the case of non-required fields) either both are blank or both are non-blank, and raises an exception otherwise.

Attachments (1)

fields.diff (832 bytes) - added by ctdecci@… 11 years ago.

Download all attachments as: .zip

Change History (6)

Changed 11 years ago by ctdecci@…

Attachment: fields.diff added

comment:1 Changed 11 years ago by Gary Wilson <gary.wilson@…>

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 Changed 11 years ago by Matt Boersma

Owner: changed from nobody to Matt Boersma
Status: newassigned

comment:3 Changed 11 years ago by Matt Boersma

Owner: Matt Boersma deleted
Status: assignednew

I wasn't able to reproduce the error given the steps described above:

In [7]: f.errors
Out[7]: {}

comment:4 in reply to:  3 Changed 11 years ago by anonymous

Resolution: duplicate
Status: newclosed

Replying to mboersma:

I wasn't able to reproduce the error given the steps described above:

In [7]: f.errors
Out[7]: {}

It appears that a duplicate ticket (#4630) was entered, and this was fixed (more elegantly, IMHO) in revision 5515:

r5515 | mtredinnick | 2007-06-22 20:32:59 -0700 (Fri, 22 Jun 2007) | 3 lines

Fixed #4630 -- Fixed some validation problems with SplitDateTimeField. Thanks
glin@… and SmileyChris.

comment:5 Changed 11 years ago by ctdecci@…

Just forgot to enter my email on last comment...

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