Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored October 08, 2010
10  django/db/models/fields/__init__.py
@@ -622,7 +622,7 @@ def to_python(self, value):
622 622
 
623 623
     def pre_save(self, model_instance, add):
624 624
         if self.auto_now or (self.auto_now_add and add):
625  
-            value = datetime.datetime.now()
  625
+            value = datetime.date.today()
626 626
             setattr(model_instance, self.attname, value)
627 627
             return value
628 628
         else:
@@ -709,6 +709,14 @@ def to_python(self, value):
709 709
                 except ValueError:
710 710
                     raise exceptions.ValidationError(self.error_messages['invalid'])
711 711
 
  712
+    def pre_save(self, model_instance, add):
  713
+        if self.auto_now or (self.auto_now_add and add):
  714
+            value = datetime.datetime.now()
  715
+            setattr(model_instance, self.attname, value)
  716
+            return value
  717
+        else:
  718
+            return super(DateTimeField, self).pre_save(model_instance, add)
  719
+
712 720
     def get_prep_value(self, value):
713 721
         return self.to_python(value)
714 722
 
4  tests/regressiontests/datatypes/models.py
@@ -19,3 +19,7 @@ class Meta:
19 19
 
20 20
     def __str__(self):
21 21
         return self.name
  22
+
  23
+class RumBaba(models.Model):
  24
+    baked_date = models.DateField(auto_now_add=True)
  25
+    baked_timestamp = models.DateTimeField(auto_now_add=True)
22  tests/regressiontests/datatypes/tests.py
@@ -3,7 +3,7 @@
3 3
 from django.test import TestCase
4 4
 from django.utils import tzinfo
5 5
 
6  
-from models import Donut
  6
+from models import Donut, RumBaba
7 7
 from django.conf import settings
8 8
 
9 9
 class DataTypesTestCase(TestCase):
@@ -43,7 +43,7 @@ def test_time_field(self):
43 43
         self.assertEqual(d2.baked_time, datetime.time(16, 19, 59))
44 44
 
45 45
     def test_year_boundaries(self):
46  
-        # Year boundary tests (ticket #3689)
  46
+        """Year boundary tests (ticket #3689)"""
47 47
         d = Donut.objects.create(name='Date Test 2007',
48 48
              baked_date=datetime.datetime(year=2007, month=12, day=31),
49 49
              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):
67 67
         self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count())
68 68
 
69 69
     def test_textfields_unicode(self):
70  
-        # Regression test for #10238: TextField values returned from the database
71  
-        # should be unicode.
  70
+        """Regression test for #10238: TextField values returned from the
  71
+        database should be unicode."""
72 72
         d = Donut.objects.create(name=u'Jelly Donut', review=u'Outstanding')
73 73
         newd = Donut.objects.get(id=d.id)
74 74
         self.assert_(isinstance(newd.review, unicode))
75 75
 
76 76
     def test_tz_awareness_mysql(self):
77  
-        # Regression test for #8354: the MySQL backend should raise an error if given
78  
-        # a timezone-aware datetime object.
  77
+        """Regression test for #8354: the MySQL backend should raise an error
  78
+        if given a timezone-aware datetime object."""
79 79
         if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.mysql':
80 80
             dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0))
81 81
             d = Donut(name='Bear claw', consumed_at=dt)
82 82
             self.assertRaises(ValueError, d.save)
83 83
             # ValueError: MySQL backend does not support timezone-aware datetimes.
  84
+
  85
+    def test_datefield_auto_now_add(self):
  86
+        """Regression test for #10970, auto_now_add for DateField should store
  87
+        a Python datetime.date, not a datetime.datetime"""
  88
+        b = RumBaba.objects.create()
  89
+        # Verify we didn't break DateTimeField behavior
  90
+        self.assert_(isinstance(b.baked_timestamp, datetime.datetime))
  91
+        # We need to test this this way because datetime.datetime inherits
  92
+        # from datetime.date:
  93
+        self.assert_(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime))

0 notes on commit dbf23ef

Please sign in to comment.
Something went wrong with that request. Please try again.