Permalink
Browse files

Don't use auto_now_add and auto_now

  • Loading branch information...
1 parent 29c7089 commit a3cf1f7b2e9dbe4964306f4793c74f1782f8b2ea @jedie committed May 31, 2012
Showing with 12 additions and 3 deletions.
  1. +12 −3 django_tools/models.py
View
@@ -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):

0 comments on commit a3cf1f7

Please sign in to comment.