Reputation: 979
For example, see the code below:
class Thing(Model):
def save(force=False, *args, **kwargs):
if not force:
raise Exception("don't save!")
FactoryBoy calls model.get_or_create() which calls model.save(). Is there any way to create an instance of this model in FactoryBoy without modifying the save or get_or_create methods?
Upvotes: 2
Views: 1489
Reputation: 3589
The default behavior of factory.django.DjangoModelFactory
is to call MyModel.objects.create()
.
If the goal is only for a single call in one test, just use MyModelFactory.build()
.
If the goal is to never call create()
, set the following in your declaration:
class MyModelFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.MyModel
strategy = factory.BUILD_STRATEGY
This maps MyModelFactory()
to MyModelFactory.build()
instead of the DjangoModelFactory
default, MyModelFactory.create()
.
Upvotes: 3
Reputation: 128
If all you want is a local instance of a Django model, you should use the build strategy. Also, the create strategy invokes _create
method, which can be overwritten to meet your needs.
Upvotes: 4