Code

Ticket #10970: auto_now_patch_1.diff

File auto_now_patch_1.diff, 2.0 KB (added by Cyberj, 4 years ago)

Patch for rev 12847

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 12847)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -646,7 +646,7 @@
6 
7     def pre_save(self, model_instance, add):
8         if self.auto_now or (self.auto_now_add and add):
9-            value = datetime.datetime.now()
10+            value = datetime.date.today()
11             setattr(model_instance, self.attname, value)
12             return value
13         else:
14@@ -733,6 +733,14 @@
15                 except ValueError:
16                     raise exceptions.ValidationError(self.error_messages['invalid'])
17 
18+    def pre_save(self, model_instance, add):
19+        if self.auto_now or (self.auto_now_add and add):
20+            value = datetime.datetime.now()
21+            setattr(model_instance, self.attname, value)
22+            return value
23+        else:
24+            return super(DateTimeField, self).pre_save(model_instance, add)
25+
26     def get_prep_value(self, value):
27         return self.to_python(value)
28 
29Index: tests/regressiontests/datatypes/models.py
30===================================================================
31--- tests/regressiontests/datatypes/models.py   (revision 12847)
32+++ tests/regressiontests/datatypes/models.py   (working copy)
33@@ -21,6 +21,10 @@
34     def __str__(self):
35         return self.name
36 
37+class RumBaba(models.Model):
38+    backed_date = models.DateField(auto_now_add=True)
39+    backed_time = models.DateTimeField(auto_now_add=True)
40+
41 __test__ = {'API_TESTS': """
42 # No donuts are in the system yet.
43 >>> Donut.objects.all()
44@@ -98,6 +102,15 @@
45 >>> Donut.objects.get(id=d2.id).review
46 u'Outstanding'
47 
48+# Test for #10970, auto_now_add for DateField should evaluete to datetime.date instance
49+
50+>>> b = RumBaba()
51+>>> b.save()
52+>>> isinstance(b.backed_time, datetime.datetime)
53+True
54+>>> isinstance(b.backed_date, datetime.date)
55+True
56+
57 """}
58 
59 # Regression test for #8354: the MySQL backend should raise an error if given