﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12401	DateField/DateTimeField is not instantiated with Python datetime.date/datetime object until after retrieving from the database	heidar_rafn	nobody	"The Django documentation states:[[BR]]
"" DateField[[BR]]
class DateField([auto_now=False, auto_now_add=False, **options])[[BR]]
A date, represented in Python by a datetime.date instance...""


I have experienced a problem with this - the field is not represented by the datetime.date class until after retrived from the database.

Here is a small example that shows this:

{{{
class chgLog(models.Model):
    chgReason = models.CharField(max_length=255)
    validFrom = models.DateField()
}}}

Following is from a Python/Django shell session :
{{{
>>> chglog = chgLog(chgReason='testing datefield',validFrom='2009-13-43')
>>> chglog.id
>>> chglog.validFrom
'2009-13-43'
>>> chglog.validFrom.__class__
<type 'str'>
>>> chglog.save()
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/usr/lib/pymodules/python2.6/django/db/models/base.py"", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File ""/usr/lib/pymodules/python2.6/django/db/models/base.py"", line 483, in save_base
    values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)]
  File ""/usr/lib/pymodules/python2.6/django/db/models/fields/__init__.py"", line 192, in get_db_prep_save
    return self.get_db_prep_value(value)
  File ""/usr/lib/pymodules/python2.6/django/db/models/fields/__init__.py"", line 511, in get_db_prep_value
    return connection.ops.value_to_db_date(self.to_python(value))
  File ""/usr/lib/pymodules/python2.6/django/db/models/fields/__init__.py"", line 484, in to_python
    raise exceptions.ValidationError(msg)
ValidationError: Invalid date: month must be in 1..12
}}}
It shows that validation is done properly.
Now let us have all values valid:
{{{
>>> chglog = chgLog(chgReason='testing datefield',validFrom='2009-12-18')
>>> chglog.id
>>> chglog.validFrom
'2009-12-18'
>>> chglog.validFrom.__class__
<type 'str'>
>>> chglog.save()
>>> chglog.id
130L
>>> chglog.validFrom
'2009-12-18'
>>> chglog.validFrom.__class__
<type 'str'>
}}}
And here it is obvious that the object stores the DateField as string, even after saving.
Now let us investigate it after retrieving it from the database:
{{{
>>> chglog_from_db = chgLog.objects.get(id=130)
>>> chglog_from_db.id
130L
>>> chglog_from_db.validFrom
datetime.date(2009, 12, 18)
>>> chglog_from_db.validFrom.__class__
<type 'datetime.date'>
}}}

And finally this code segments that compares DateField from two different Django objects, the first one after saving, the second one after retrieving from the database. Same object (2 copies), but reported to have different values:
{{{
>>> chglog.validFrom == chglog_from_db.validFrom
False
>>> 
}}}

"	Bug	closed	Core (Other)	1.1	Normal	invalid	DateField DateTimeField	jroesslein@…	Design decision needed	0	0	0	0	0	0
