All my models, including Report inherit from BaseModel:
class Report(BaseModel):
...
class BaseModel(models.Model):
created_date = models.DateTimeField(auto_now_add=True, db_index=True)
modified_date = models.DateTimeField(auto_now=True, db_index=True)
class Meta:
abstract = True
I'm trying to test a scheduled tasks that deletes old Report objects.
Below is a fixture:
@pytest.fixture(scope="function")
def old_dummy_report(request, db):
### set the date to far back
old_date = datetime.datetime.now() - datetime.timedelta(days=900)
return mixer.blend("core.report", , created_date=old_date, ios_report={'1': 1}, android_report={'1': 1})
However, when I run the test and inspect the created_date field for old_dummy_report, I always get the date at the time the test is run.
How can I rectify this, besides re-setting the date to old_date in the test function itself (which seems unpythonic).
Found the solution. I guess this has to do with the
auto_now_add=Trueparameter.I've changed the pytest fixture function to this:
So, first create the object and let Django
auto_now_add, then change thecreated_datemanually.