class MyModel(models.Model):
time_field = models.DateTimeField(auto_now=True)
This makes testing difficult, because the created
field will always be the current time. If you try changeing it with
myobject.time_field = _sometime
myobject.save()
it will get reset to the current time when saving.
Here's how to change the time that it gets set to in a test:
testtime = datetime.strptime('2015-10-31', '%Y-%m-%d')
with mock.patch('django.utils.timezone.now') as mock_now:
mock_now.return_value = testtime
MyModel.objects.create()
When the with
block is over, further models will be created with current time.