Ticket #16745: auto-use-transaction-time.diff

File auto-use-transaction-time.diff, 4.4 KB (added by Michael Mior, 13 years ago)
  • django/db/backends/__init__.py

    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  
    1111from django.db.backends import util
    1212from django.db.transaction import TransactionManagementError
    1313from django.utils.importlib import import_module
    14 from django.utils.timezone import is_aware
     14from django.utils import timezone
    1515
    1616
    1717class BaseDatabaseWrapper(object):
    class BaseDatabaseWrapper(object):  
    3434
    3535        # Transaction related attributes
    3636        self.transaction_state = []
     37        self.transaction_start = None
    3738        self.savepoint_state = 0
    3839        self._dirty = None
    3940        self._thread_ident = thread.get_ident()
    class BaseDatabaseWrapper(object):  
    9697        if self.transaction_state:
    9798            self.transaction_state.append(self.transaction_state[-1])
    9899        else:
     100            self.transaction_start = timezone.now()
    99101            self.transaction_state.append(settings.TRANSACTIONS_MANAGED)
    100102
    101103        if self._dirty is None:
    class BaseDatabaseWrapper(object):  
    110112        """
    111113        self._leave_transaction_management(self.is_managed())
    112114        if self.transaction_state:
     115            self.transaction_start = None
    113116            del self.transaction_state[-1]
    114117        else:
    115118            raise TransactionManagementError("This code isn't under transaction "
    class BaseDatabaseOperations(object):  
    787790        """
    788791        if value is None:
    789792            return None
    790         if is_aware(value):
     793        if timezone.is_aware(value):
    791794            raise ValueError("Django does not support timezone-aware times.")
    792795        return unicode(value)
    793796
  • django/db/models/fields/__init__.py

    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):  
    683683
    684684    def pre_save(self, model_instance, add):
    685685        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()
    687688            setattr(model_instance, self.attname, value)
    688689            return value
    689690        else:
    class DateTimeField(DateField):  
    773774
    774775    def pre_save(self, model_instance, add):
    775776        if self.auto_now or (self.auto_now_add and add):
    776             value = timezone.now()
     777            value = connection.transaction_start or timezone.now()
    777778            setattr(model_instance, self.attname, value)
    778779            return value
    779780        else:
    class TimeField(Field):  
    12061207
    12071208    def pre_save(self, model_instance, add):
    12081209        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()
    12101211            setattr(model_instance, self.attname, value)
    12111212            return value
    12121213        else:
  • tests/regressiontests/datatypes/models.py

    diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py
    index 332ce84..aacd22f 100644
    a b class Donut(models.Model):  
    2424class RumBaba(models.Model):
    2525    baked_date = models.DateField(auto_now_add=True)
    2626    baked_timestamp = models.DateTimeField(auto_now_add=True)
     27    fresh_date = models.DateField(auto_now=True)
     28    fresh_timestamp = models.DateTimeField(auto_now=True)
  • tests/regressiontests/datatypes/tests.py

    diff --git a/tests/regressiontests/datatypes/tests.py b/tests/regressiontests/datatypes/tests.py
    index fb94e83..f23a8a2 100644
    a b class DataTypesTestCase(TestCase):  
    9393        # We need to test this this way because datetime.datetime inherits
    9494        # from datetime.date:
    9595        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)
Back to Top