Why does the naming of RelatedFactory variables in factory-boy affect when the factories are executed/instantiated?

梦想的初衷 提交于 2019-12-23 16:01:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!