From dbf23ef273ae6c8999cdaa9b6d81e98476d753db Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 8 Oct 2010 16:14:10 +0000 Subject: [PATCH] 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 --- django/db/models/fields/__init__.py | 10 +++++++++- tests/regressiontests/datatypes/models.py | 4 ++++ tests/regressiontests/datatypes/tests.py | 22 ++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 3c588730356..e5ed44dec5d 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -622,7 +622,7 @@ class DateField(Field): def pre_save(self, model_instance, 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) return value else: @@ -709,6 +709,14 @@ class DateTimeField(DateField): except ValueError: 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): return self.to_python(value) diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py index 47834b73cd4..f6fb72dbea6 100644 --- a/tests/regressiontests/datatypes/models.py +++ b/tests/regressiontests/datatypes/models.py @@ -19,3 +19,7 @@ class Donut(models.Model): def __str__(self): return self.name + +class RumBaba(models.Model): + baked_date = models.DateField(auto_now_add=True) + baked_timestamp = models.DateTimeField(auto_now_add=True) diff --git a/tests/regressiontests/datatypes/tests.py b/tests/regressiontests/datatypes/tests.py index fc8085c8935..f7a0447ac10 100644 --- a/tests/regressiontests/datatypes/tests.py +++ b/tests/regressiontests/datatypes/tests.py @@ -3,7 +3,7 @@ from django.db import DEFAULT_DB_ALIAS from django.test import TestCase from django.utils import tzinfo -from models import Donut +from models import Donut, RumBaba from django.conf import settings class DataTypesTestCase(TestCase): @@ -43,7 +43,7 @@ class DataTypesTestCase(TestCase): self.assertEqual(d2.baked_time, datetime.time(16, 19, 59)) def test_year_boundaries(self): - # Year boundary tests (ticket #3689) + """Year boundary tests (ticket #3689)""" d = Donut.objects.create(name='Date Test 2007', 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)) @@ -67,17 +67,27 @@ class DataTypesTestCase(TestCase): self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count()) def test_textfields_unicode(self): - # Regression test for #10238: TextField values returned from the database - # should be unicode. + """Regression test for #10238: TextField values returned from the + database should be unicode.""" d = Donut.objects.create(name=u'Jelly Donut', review=u'Outstanding') newd = Donut.objects.get(id=d.id) self.assert_(isinstance(newd.review, unicode)) def test_tz_awareness_mysql(self): - # Regression test for #8354: the MySQL backend should raise an error if given - # a timezone-aware datetime object. + """Regression test for #8354: the MySQL backend should raise an error + if given a timezone-aware datetime object.""" if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.mysql': dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0)) d = Donut(name='Bear claw', consumed_at=dt) self.assertRaises(ValueError, d.save) # 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))