Opened 19 years ago
Closed 19 years ago
#3172 closed defect (fixed)
Model.validate raises TypeError on NULL for DateField, DateTimeField, and TimeField
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | normal | Keywords: | |
| Cc: | sam@…, floguy@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When you have a model with any of the date/time fields, it throws a TypeError. For example:
class TestModel(models.Model):
test_date = models.DateField()
test_date_time = models.DateTimeField()
test_time = models.TimeField()
TestModel().validate()
Traceback (most recent call last):
File "<console>", line 1, in ?
File "django/db/models/base.py", line 230, in validate
setattr(self, f.attname, f.to_python(getattr(self, f.attname, f.get_default())))
File "django/db/models/fields/__init__.py", line 435, in to_python
validators.isValidANSIDate(value, None)
File "django/core/validators.py", line 146, in isValidANSIDate
if not ansi_date_re.search(field_data):
TypeError: expected string or buffer
Similar errors appear with DateTime and Time.
Attachments (1)
Change History (9)
comment:1 by , 19 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:2 by , 19 years ago
| Resolution: | invalid |
|---|---|
| Status: | closed → reopened |
| Summary: | Model.validate fails on NULL for DateField, DateTimeField, and TimeField → Model.validate raises TypeError on NULL for DateField, DateTimeField, and TimeField |
I'm sorry, I guess I didn't explain myself properly. validate() does not return a dict of problems, it throws a TypeError. Changing models.DateField() to models.DateField(null=True, blank=True) doesn't help either.
comment:3 by , 19 years ago
Diff for your viewing pleasure:
Index: django/db/models/fields/__init__.py
===================================================================
--- django/db/models/fields/__init__.py (revision 4236)
+++ django/db/models/fields/__init__.py (working copy)
@@ -428,6 +428,8 @@
Field.__init__(self, verbose_name, name, **kwargs)
def to_python(self, value):
+ if value is None:
+ return value
if isinstance(value, datetime.datetime):
return value.date()
if isinstance(value, datetime.date):
@@ -488,6 +490,8 @@
class DateTimeField(DateField):
def to_python(self, value):
+ if value is None:
+ return value
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
comment:5 by , 19 years ago
| Cc: | added |
|---|
comment:6 by , 19 years ago
| Has patch: | set |
|---|---|
| Needs tests: | set |
| Triage Stage: | Unreviewed → Ready for checkin |
It's obviously a bug. Probably could do with a regression test
by , 19 years ago
| Attachment: | datetime_validation_fix_and_tests.patch added |
|---|
Updated patch to revision 4427 and added unit tests.
comment:7 by , 19 years ago
| Cc: | added |
|---|---|
| Needs tests: | unset |
I have updated the patch which was posted here to revision 4427. Also, I have taken that test case which was given in the original post and converted it to a unit test which is also included in the patch. As long as this patch applies cleanly for others, it should be ready for production now. All regression tests pass as well.
comment:8 by , 19 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
Behaviour is as designed. TestModel() is a new, empty instance. The fields test_date, test_date_time and test_time don't yet have values. As a result, validation fails, as the fields do not have values that are a string or a DateTime instance.