From a3cf1f7b2e9dbe4964306f4793c74f1782f8b2ea Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Thu, 31 May 2012 14:02:20 +0200 Subject: [PATCH] Don't use auto_now_add and auto_now --- django_tools/models.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/django_tools/models.py b/django_tools/models.py index 69b6c2c..eac41cc 100644 --- a/django_tools/models.py +++ b/django_tools/models.py @@ -11,7 +11,11 @@ from django.db import models from django.contrib.auth.models import User - +try: + from django.utils.timezone import now +except ImportError: + from datetime import datetime + now = datetime.now from django_tools.middlewares import ThreadLocal @@ -23,11 +27,15 @@ class UpdateInfoBaseModel(models.Model): Base model with update info attributes, used by many models. The createby and lastupdateby ForeignKey would be automaticly updated. This needs the request object as the first argument in the save method. + + We doesn't used auto_now_add and auto_now here, because they have the odd side effect + see also: + https://github.com/jezdez/django-dbtemplates/commit/2f27327bebe7f2e7b33e5cfb0db517f53a1b9701#commitcomment-1396126 """ objects = models.Manager() - createtime = models.DateTimeField(auto_now_add=True, help_text="Create time") - lastupdatetime = models.DateTimeField(auto_now=True, help_text="Time of the last change.") + createtime = models.DateTimeField(default=now, editable=False, help_text="Create time") + lastupdatetime = models.DateTimeField(default=now, editable=False, help_text="Time of the last change.") createby = models.ForeignKey(User, editable=False, related_name="%(class)s_createby", null=True, blank=True, # <- If the model used outsite a real request (e.g. unittest, db shell) @@ -41,6 +49,7 @@ def save(self, *args, **kwargs): Automatic update createby and lastupdateby attributes with the request object witch must be the first argument. """ + self.last_changed = now() current_user = ThreadLocal.get_current_user() if current_user and isinstance(current_user, User):