I have a Django model field with auto_now_add=True. How do I override the value for this field in my test?
class ABC(models.Model):
sent_at = models.DateTimeField(auto_now_add=True)
class ABCFactory(factory.django.DjangoModelFactory):
sent_at = timezone.now() - timezone.timedelta(seconds=40)
Maybe checkout this solution posted in the factory_boy github: https://github.com/FactoryBoy/factory_boy/issues/102
In short, there are 2 solutions:
auto_now_add
in your model field and instead use default=timezone.now
. It will be fairly similar, except the field wont be read-only in the admin. But now you can easily override/update it_create
method so that it sets the field and re-save the instance AFTER it was initially created