Fixed #10970 -- Initialize DateFields with datetime.date objects, not datetime.datetime. Thanks, summerisgone, Cyberj and Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14029 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-10-08 16:14:10 +00:00
parent ae128aa28b
commit dbf23ef273
3 changed files with 29 additions and 7 deletions

View File

@ -622,7 +622,7 @@ class DateField(Field):
def pre_save(self, model_instance, add): def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add): if self.auto_now or (self.auto_now_add and add):
value = datetime.datetime.now() value = datetime.date.today()
setattr(model_instance, self.attname, value) setattr(model_instance, self.attname, value)
return value return value
else: else:
@ -709,6 +709,14 @@ class DateTimeField(DateField):
except ValueError: except ValueError:
raise exceptions.ValidationError(self.error_messages['invalid']) raise exceptions.ValidationError(self.error_messages['invalid'])
def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add):
value = datetime.datetime.now()
setattr(model_instance, self.attname, value)
return value
else:
return super(DateTimeField, self).pre_save(model_instance, add)
def get_prep_value(self, value): def get_prep_value(self, value):
return self.to_python(value) return self.to_python(value)

View File

@ -19,3 +19,7 @@ class Donut(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
class RumBaba(models.Model):
baked_date = models.DateField(auto_now_add=True)
baked_timestamp = models.DateTimeField(auto_now_add=True)

View File

@ -3,7 +3,7 @@ from django.db import DEFAULT_DB_ALIAS
from django.test import TestCase from django.test import TestCase
from django.utils import tzinfo from django.utils import tzinfo
from models import Donut from models import Donut, RumBaba
from django.conf import settings from django.conf import settings
class DataTypesTestCase(TestCase): class DataTypesTestCase(TestCase):
@ -43,7 +43,7 @@ class DataTypesTestCase(TestCase):
self.assertEqual(d2.baked_time, datetime.time(16, 19, 59)) self.assertEqual(d2.baked_time, datetime.time(16, 19, 59))
def test_year_boundaries(self): def test_year_boundaries(self):
# Year boundary tests (ticket #3689) """Year boundary tests (ticket #3689)"""
d = Donut.objects.create(name='Date Test 2007', d = Donut.objects.create(name='Date Test 2007',
baked_date=datetime.datetime(year=2007, month=12, day=31), baked_date=datetime.datetime(year=2007, month=12, day=31),
consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59)) consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59))
@ -67,17 +67,27 @@ class DataTypesTestCase(TestCase):
self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count()) self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count())
def test_textfields_unicode(self): def test_textfields_unicode(self):
# Regression test for #10238: TextField values returned from the database """Regression test for #10238: TextField values returned from the
# should be unicode. database should be unicode."""
d = Donut.objects.create(name=u'Jelly Donut', review=u'Outstanding') d = Donut.objects.create(name=u'Jelly Donut', review=u'Outstanding')
newd = Donut.objects.get(id=d.id) newd = Donut.objects.get(id=d.id)
self.assert_(isinstance(newd.review, unicode)) self.assert_(isinstance(newd.review, unicode))
def test_tz_awareness_mysql(self): def test_tz_awareness_mysql(self):
# Regression test for #8354: the MySQL backend should raise an error if given """Regression test for #8354: the MySQL backend should raise an error
# a timezone-aware datetime object. if given a timezone-aware datetime object."""
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.mysql': if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.mysql':
dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0)) dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0))
d = Donut(name='Bear claw', consumed_at=dt) d = Donut(name='Bear claw', consumed_at=dt)
self.assertRaises(ValueError, d.save) self.assertRaises(ValueError, d.save)
# ValueError: MySQL backend does not support timezone-aware datetimes. # ValueError: MySQL backend does not support timezone-aware datetimes.
def test_datefield_auto_now_add(self):
"""Regression test for #10970, auto_now_add for DateField should store
a Python datetime.date, not a datetime.datetime"""
b = RumBaba.objects.create()
# Verify we didn't break DateTimeField behavior
self.assert_(isinstance(b.baked_timestamp, datetime.datetime))
# We need to test this this way because datetime.datetime inherits
# from datetime.date:
self.assert_(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime))