#4317 closed (duplicate)
Fix compress() in SplitDateTimeField
Reported by: | Owned by: | ||
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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/forms.py", line 93, in _errors self.full_clean() File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line 187, in full_clean value = field.clean(value) File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line 473, in clean return self.compress(clean_data) File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line 493, in compress return datetime.datetime.combine(*data_list) TypeError: combine() argument 1 must be datetime.date, 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)
Change History (6)
by , 18 years ago
Attachment: | fields.diff added |
---|
comment:1 by , 18 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
follow-up: 4 comment:3 by , 17 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:4 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Replying to mboersma:
I wasn't able to reproduce the error given the steps described above:
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.
I wasn't able to reproduce the error given the steps described above:
In [7]: f.errors
Out[7]: {}