问题
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..
来源:https://stackoverflow.com/questions/33444692/why-does-the-naming-of-relatedfactory-variables-in-factory-boy-affect-when-the-f