问题
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