diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index ebe8875..8db0baf 100644
a
|
b
|
from django.db import DEFAULT_DB_ALIAS
|
11 | 11 | from django.db.backends import util |
12 | 12 | from django.db.transaction import TransactionManagementError |
13 | 13 | from django.utils.importlib import import_module |
14 | | from django.utils.timezone import is_aware |
| 14 | from django.utils import timezone |
15 | 15 | |
16 | 16 | |
17 | 17 | class BaseDatabaseWrapper(object): |
… |
… |
class BaseDatabaseWrapper(object):
|
34 | 34 | |
35 | 35 | # Transaction related attributes |
36 | 36 | self.transaction_state = [] |
| 37 | self.transaction_start = None |
37 | 38 | self.savepoint_state = 0 |
38 | 39 | self._dirty = None |
39 | 40 | self._thread_ident = thread.get_ident() |
… |
… |
class BaseDatabaseWrapper(object):
|
96 | 97 | if self.transaction_state: |
97 | 98 | self.transaction_state.append(self.transaction_state[-1]) |
98 | 99 | else: |
| 100 | self.transaction_start = timezone.now() |
99 | 101 | self.transaction_state.append(settings.TRANSACTIONS_MANAGED) |
100 | 102 | |
101 | 103 | if self._dirty is None: |
… |
… |
class BaseDatabaseWrapper(object):
|
110 | 112 | """ |
111 | 113 | self._leave_transaction_management(self.is_managed()) |
112 | 114 | if self.transaction_state: |
| 115 | self.transaction_start = None |
113 | 116 | del self.transaction_state[-1] |
114 | 117 | else: |
115 | 118 | raise TransactionManagementError("This code isn't under transaction " |
… |
… |
class BaseDatabaseOperations(object):
|
787 | 790 | """ |
788 | 791 | if value is None: |
789 | 792 | return None |
790 | | if is_aware(value): |
| 793 | if timezone.is_aware(value): |
791 | 794 | raise ValueError("Django does not support timezone-aware times.") |
792 | 795 | return unicode(value) |
793 | 796 | |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 04b13aa..19d01ff 100644
a
|
b
|
class DateField(Field):
|
683 | 683 | |
684 | 684 | def pre_save(self, model_instance, add): |
685 | 685 | if self.auto_now or (self.auto_now_add and add): |
686 | | value = datetime.date.today() |
| 686 | value = connection.transaction_start or timezone.now() |
| 687 | value = value.date() |
687 | 688 | setattr(model_instance, self.attname, value) |
688 | 689 | return value |
689 | 690 | else: |
… |
… |
class DateTimeField(DateField):
|
773 | 774 | |
774 | 775 | def pre_save(self, model_instance, add): |
775 | 776 | if self.auto_now or (self.auto_now_add and add): |
776 | | value = timezone.now() |
| 777 | value = connection.transaction_start or timezone.now() |
777 | 778 | setattr(model_instance, self.attname, value) |
778 | 779 | return value |
779 | 780 | else: |
… |
… |
class TimeField(Field):
|
1206 | 1207 | |
1207 | 1208 | def pre_save(self, model_instance, add): |
1208 | 1209 | if self.auto_now or (self.auto_now_add and add): |
1209 | | value = datetime.datetime.now().time() |
| 1210 | value = (connection.transaction_start or timezone.now()).time() |
1210 | 1211 | setattr(model_instance, self.attname, value) |
1211 | 1212 | return value |
1212 | 1213 | else: |
diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py
index 332ce84..aacd22f 100644
a
|
b
|
class Donut(models.Model):
|
24 | 24 | class RumBaba(models.Model): |
25 | 25 | baked_date = models.DateField(auto_now_add=True) |
26 | 26 | baked_timestamp = models.DateTimeField(auto_now_add=True) |
| 27 | fresh_date = models.DateField(auto_now=True) |
| 28 | fresh_timestamp = models.DateTimeField(auto_now=True) |
diff --git a/tests/regressiontests/datatypes/tests.py b/tests/regressiontests/datatypes/tests.py
index fb94e83..f23a8a2 100644
a
|
b
|
class DataTypesTestCase(TestCase):
|
93 | 93 | # We need to test this this way because datetime.datetime inherits |
94 | 94 | # from datetime.date: |
95 | 95 | self.assertTrue(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime)) |
| 96 | |
| 97 | def test_datefield_auto_now(self): |
| 98 | """Test for #16745, auto_now and auto_now_add should produce identical |
| 99 | values when the model is first created.""" |
| 100 | b = RumBaba.objects.create() |
| 101 | |
| 102 | self.assertEqual(b.baked_timestamp, b.fresh_timestamp) |
| 103 | self.assertEqual(b.baked_date, b.fresh_date) |