When I named my RelatedFactory variables pv_something
, the factories wouldn't be run until after the post_generation
method. When I renamed the variables to param_val_something
they would be run before the post_generation
method.
In the following code, RelatedFactory
is not run until after post_generation
, so self.something_set.all()
is empty, and the line t.something_else = 'abc'
is never executed.
class ThingFactory(factory.DjangoModelFactory):
class Meta:
model = Thing
name = 'a thing'
pv_something = factory.RelatedFactory(SomethingFactory, 'thing')
@factory.post_generation
def post(self, create, extracted, **kwargs):
for t in self.something_set.all():
t.something_else = 'abc'
In the following code, the only difference is renaming the variable pv_something
to param_val_something
. Now, self.something_set.all()
is not empty, and the line t.something_else = 'abc'
is executed.
class ThingFactory(factory.DjangoModelFactory):
class Meta:
model = Thing
name = 'a thing'
param_val_something = factory.RelatedFactory(SomethingFactory, 'thing')
@factory.post_generation
def post(self, create, extracted, **kwargs):
for t in self.something_set.all():
t.something_else = 'abc'
I'm using Python 3.4.3, Django 1.8.5, and factory-boy 2.5.2.
Midnight Friday night, this nearly sent me over the edge..
self.something_set
list, butSomething.objects.filter(thing_id=self.id)
also failed to retrieve the RelatedFactory records. – Caprifoliaceousfactory.RelatedFactory(SomethingFactory, 'thing')
calls into the post-gen method aspv_something = SomethingFactory(thing=self)
. The order of RelatedFactory and post-gen doesn't seem to be guaranteed - presumably a consequence of some lazy evaluation somewhere. – Caprifoliaceous