Ticket #10970: auto_now_patch.diff
File auto_now_patch.diff, 2.1 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/__init__.py
485 485 486 486 def pre_save(self, model_instance, add): 487 487 if self.auto_now or (self.auto_now_add and add): 488 value = datetime.date time.now()488 value = datetime.date.today() 489 489 setattr(model_instance, self.attname, value) 490 490 return value 491 491 else: … … 564 564 raise exceptions.ValidationError( 565 565 _('Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.')) 566 566 567 def pre_save(self, model_instance, add): 568 if self.auto_now or (self.auto_now_add and add): 569 value = datetime.datetime.now() 570 setattr(model_instance, self.attname, value) 571 return value 572 else: 573 return super(DateTimeField, self).pre_save(model_instance, add) 574 567 575 def get_db_prep_value(self, value): 568 576 # Casts dates into the format expected by the backend 569 577 return connection.ops.value_to_db_datetime(self.to_python(value)) -
tests/regressiontests/datatypes/models.py
21 21 def __str__(self): 22 22 return self.name 23 23 24 class RumBaba(models.Model): 25 backed_date = models.DateField(auto_now_add=True) 26 backed_time = models.DateTimeField(auto_now_add=True) 27 24 28 __test__ = {'API_TESTS': """ 25 29 # No donuts are in the system yet. 26 30 >>> Donut.objects.all() … … 89 93 >>> Donut.objects.get(id=d2.id).review 90 94 u'Outstanding' 91 95 96 # Test for #10970, auto_now_add for DateField should evaluete to datetime.date instance 97 98 >>> b = RumBaba() 99 >>> b.save() 100 >>> isinstance(b.backed_time, datetime.datetime) 101 True 102 >>> isinstance(b.backed_date, datetime.date) 103 True 104 92 105 """} 93 106 94 107 # Regression test for #8354: the MySQL backend should raise an error if given