本文介绍了将特征值传递给SubFactory Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个工厂.

class DispatchDataFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = models.DispatchData

    order = factory.SelfAttribute('order_data.order')
    sku = factory.LazyAttribute(lambda obj: '%d' % obj.order_data.sku)
    category = SKUCategory.SINGLE
    quantity = 50

    class Params:
        combo_sku=False
        order_data = factory.SubFactory(OrderDataFactory, combo_sku=factory.SelfAttribute('combo_sku'))
        combo_sku = factory.Trait(
            sku=factory.LazyAttribute(lambda obj: '%d' % obj.order_data.sku),
            category=SKUCategory.COMBO,
            quantity=1
        )
class OrderDataFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = models.OrderData

    order = factory.SubFactory(OrderFactory)
    category = SKUCategory.SINGLE
    quantity = 75.6
    price_per_kg = 10.5
    sku = factory.SelfAttribute('crop_data.id')

    class Params:
        crop_data = factory.SubFactory(CropFactory)
        combo_data = factory.SubFactory(ComboSkuFactory)
        combo_sku = factory.Trait(
            sku=factory.SelfAttribute('combo_data.id'),
            category=SKUCategory.COMBO,
            quantity=1,
            price_per_kg=34.56
        )

因此,如果combo_sku为True,则必须在OrderDataFactory中的combo_sku上.

so if combo_sku is True then it must on combo_sku in OrderDataFactory.

我遇到以下错误.

'combo_sku'的循环惰性属性定义;在['category','combo_sku']

Cyclic lazy attribute definition for 'combo_sku'; cycle found in ['category', 'combo_sku']

还有其他方法可以将特征值传递给SubFactory.

Is there any other way to pass trait value to SubFactory.

推荐答案

我已经为first factory添加了默认值,并且可以正常工作.

I have added a default value to first factory and it working fine.

combo_sku = False

    class Params:
        order_data = factory.SubFactory(
            OrderDataFactory, combo_sku=factory.SelfAttribute('..combo_sku'))

这篇关于将特征值传递给SubFactory Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:16