Skip to content

Commit

Permalink
Don't use auto_now_add and auto_now
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed May 31, 2012
1 parent 29c7089 commit a3cf1f7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions django_tools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit a3cf1f7

Please sign in to comment.