Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit dbf23ef273ae6c8999cdaa9b6d81e98476d753db 1 parent ae128aa
@jezdez jezdez authored
View
10 django/db/models/fields/__init__.py
@@ -622,7 +622,7 @@ def to_python(self, value):
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 @@ def to_python(self, value):
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)
View
4 tests/regressiontests/datatypes/models.py
@@ -19,3 +19,7 @@ class Meta:
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)
View
22 tests/regressiontests/datatypes/tests.py
@@ -3,7 +3,7 @@
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 @@ def test_time_field(self):
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 @@ def test_year_boundaries(self):
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))
Please sign in to comment.
Something went wrong with that request. Please try again.