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
|
| 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):
|
| 97 | 97 | if self.transaction_state: |
| 98 | 98 | self.transaction_state.append(self.transaction_state[-1]) |
| 99 | 99 | else: |
| 100 | | self.transaction_start = datetime_safe.datetime.now() |
| | 100 | self.transaction_start = timezone.now() |
| 101 | 101 | self.transaction_state.append(settings.TRANSACTIONS_MANAGED) |
| 102 | 102 | |
| 103 | 103 | if self._dirty is None: |
| … |
… |
class BaseDatabaseOperations(object):
|
| 790 | 790 | """ |
| 791 | 791 | if value is None: |
| 792 | 792 | return None |
| 793 | | if is_aware(value): |
| | 793 | if timezone.is_aware(value): |
| 794 | 794 | raise ValueError("Django does not support timezone-aware times.") |
| 795 | 795 | return unicode(value) |
| 796 | 796 | |
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):
|
| 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 = (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() |
| 687 | 687 | setattr(model_instance, self.attname, value) |
| 688 | 688 | return value |
| 689 | 689 | else: |
| … |
… |
class TimeField(Field):
|
| 1206 | 1206 | |
| 1207 | 1207 | def pre_save(self, model_instance, add): |
| 1208 | 1208 | 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() |
| 1210 | 1210 | setattr(model_instance, self.attname, value) |
| 1211 | 1211 | return value |
| 1212 | 1212 | else: |