简单模型(models.py):
from django.db import models
class MyModel(models.Model):
start_date = models.DateField()
简单工厂(test_factories.py):
from datetime import date
import factory
from .models import MyModel
class MyModelFactory(factory.django.DjangoModelFactory):
class Meta:
model = MyModel
start_date = date.today()
在manage.py shell
:
In [1]: from datetime import date
In [2]: from freezegun import freeze_time
In [3]: from polls.test_factories import MyModelFactory
In [4]: date.today()
Out[4]: datetime.date(2017, 8, 16)
In [5]: with freeze_time(date(1999,9,9)):
...: print(date.today())
...: m = MyModelFactory()
...: print(m.start_date)
...:
1999-09-09
2017-08-16
当前日期是 2017-08-16,假日期是 1999-09-09。里面freeze_time
,date.today()
给假日期,但factoryboy不受freezegun的影响。它仍然给出真实的当前日期。
这是错误吗?如果是,factoryboy 或 freezegun 的错误?
如何解决这个问题?换句话说,如何让factoryboy给出假约会?(现在,我
MyModelFactory(start_date=date.today())
用来创建带有假日期的模型。)
冷冻枪版本:0.3.9
工厂男孩版本:2.8.1