3

当我命名我的 RelatedFactory variablespv_something时,工厂不会在post_generation方法之后运行。当我将变量重命名为它们时,param_val_something它们将在post_generation方法之前运行。

在下面的代码中,RelatedFactory直到 after 才运行post_generation,所以self.something_set.all()是空的,并且该行t.something_else = 'abc'永远不会执行。

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'

在以下代码中,唯一的区别是将变量重命名pv_somethingparam_val_something. 现在,self.something_set.all()不为空,并且该行t.something_else = 'abc' 执行

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'

我正在使用 Python 3.4.3、Django 1.8.5 和 factory-boy 2.5.2。

星期五晚上的午夜,这几乎把我逼到了边缘..

4

0 回答 0