diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
a
|
b
|
|
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 | 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 | |
diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py
a
|
b
|
|
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) |
diff --git a/tests/regressiontests/datatypes/tests.py b/tests/regressiontests/datatypes/tests.py
a
|
b
|
|
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): |
… |
… |
|
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 | """Test for #10970, auto_now_add for DateField should store a Python datetime.date, not a datetime.datetime""" |
| 87 | b = RumBaba.objects.create() |
| 88 | # Verify we didn't break DateTimeField behavior |
| 89 | self.assert_(isinstance(b.baked_timestamp, datetime.datetime)) |
| 90 | # We need to test this this way because datetime.datetime inherits |
| 91 | # from datetime.date: |
| 92 | self.assert_(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime)) |