Skip to content

Instantly share code, notes, and snippets.

@UBarney
Created March 22, 2018 06:32
Show Gist options
  • Save UBarney/d5d0003f87b1436489923f5a5d93710c to your computer and use it in GitHub Desktop.
Save UBarney/d5d0003f87b1436489923f5a5d93710c to your computer and use it in GitHub Desktop.
force update field of auto now
# from https://stackoverflow.com/questions/7499767/temporarily-disable-auto-now-auto-now-add
# my model
class FooBar(models.Model):
title = models.CharField(max_length=255)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=True)
# my tests
foo = FooBar.objects.get(pk=1)
# force a timestamp
lastweek = datetime.datetime.now() - datetime.timedelta(days=7)
FooBar.objects.filter(pk=foo.pk).update(updated_at=lastweek)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment