Passing Trait Value to SubFactory Django

半腔热情 提交于 2020-04-30 09:28:08

问题


I have two factories.

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
        )

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

I am getting following error.

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

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


回答1:


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'))


来源:https://stackoverflow.com/questions/61438138/passing-trait-value-to-subfactory-django

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