Ticket #16745: x.patch

File x.patch, 2.6 KB (added by Michael Mior, 13 years ago)

patch

  • django/db/backends/__init__.py

    commit 65c6434b59efe33c3484556c35e9c737ef99400d
    Author: Michael Mior <michael.mior@gmail.com>
    Date:   Sun Jan 8 22:37:25 2012 -0500
    
        Match automatically updated times on models
        
        On a model with multiple fields with auto_now_add or auto_now, the time
        may differ on each save. Instead, we record the time when a transaction
        starts and use this time whenever a value is required.
    
    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index f9cd9b9..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):  
    9797        if self.transaction_state:
    9898            self.transaction_state.append(self.transaction_state[-1])
    9999        else:
    100             self.transaction_start = datetime_safe.datetime.now()
     100            self.transaction_start = timezone.now()
    101101            self.transaction_state.append(settings.TRANSACTIONS_MANAGED)
    102102
    103103        if self._dirty is None:
    class BaseDatabaseOperations(object):  
    790790        """
    791791        if value is None:
    792792            return None
    793         if is_aware(value):
     793        if timezone.is_aware(value):
    794794            raise ValueError("Django does not support timezone-aware times.")
    795795        return unicode(value)
    796796
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 5663d6d..8f5cff9 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 = (connection.transaction_start and connection.transaction_start.date()) or datetime.date.today()
     686            value = (connection.transaction_start and connection.transaction_start.date()) or timezone.now().date()
    687687            setattr(model_instance, self.attname, value)
    688688            return value
    689689        else:
    class TimeField(Field):  
    12061206
    12071207    def pre_save(self, model_instance, add):
    12081208        if self.auto_now or (self.auto_now_add and add):
    1209             value = (connection.transaction_start or datetime.datetime.now()).time()
     1209            value = (connection.transaction_start or timezone.now()).time()
    12101210            setattr(model_instance, self.attname, value)
    12111211            return value
    12121212        else:
Back to Top