由于django 模型中的auto_now_add,sent_at在 pytest 工厂中不起作用。需要覆盖 pytest 工厂的 sent_at
class ABC(models.Model):
x = models.ForeignKey(X, on_delete=models.CASCADE, related_name="xx")
y = models.ForeignKey(Y, on_delete=models.CASCADE, related_name="yy")
sent_at = models.DateTimeField(auto_now_add=True)
class ABCFactory(factory.django.DjangoModelFactory):
x = factory.SubFactory(XFactory)
y = factory.SubFactory(YFactory)
sent_at = timezone.now() - timezone.timedelta(seconds=40)
Run Code Online (Sandbox Code Playgroud) django unit-testing pytest django-rest-framework pytest-django