【发布时间】:2020-05-15 15:49:20
【问题描述】:
由于在模型中创建记录之前不知道通用外键对象的类型,我将其定义为什么子工厂?还是有其他方法可以解决这个问题?
models.py
class Contract(models.Model):
offer_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
offer_id = models.PositiveIntegerField()
offer = GenericForeignKey('offer_type', 'offer_id')
invoice = models.ForeignKey('Invoice', on_delete=models.PROTECT)
status = models.CharField(max_length=8)
commission = models.DecimalField(max_digits=100, decimal_places=2)
factories.py
class ContractFactory(factory.DjangoModelFactory):
class Meta:
model = models.Contract
#What to do here ???
offer = factory.SubFactory(????)
invoice = factory.SubFactory(InvoiceFactory)
status = 'active'
commission = 40.00
【问题讨论】:
标签: python-3.x unit-testing factory-boy