我使用 Model Mommy 来创建测试数据,到目前为止它运行良好。
现在我有一个名为 Django 的模型Invoice
和一个名为InvoiceItem
.
class Invoice(models.Model):
created_by = models.ForeignKey(users.User)
class InvoiceItem(models.Model):
invoice = models.ForeignKey(Invoice, related_name='items')
我为这些模型创建了两个简单的妈妈食谱:
InvoiceRecipe = Recipe(
Invoice,
created_by=foreign_key(UserRecipe),
)
InvoiceItemRecipe = Recipe(
InvoiceItem,
invoice=foreign_key(InvoiceRecipe),
)
现在我想InvoiceRecipe.make()
自动将不同数量的InvoiceItem
对象添加到创建的发票中。
目前,我正在这样做,但我希望它自动发生:
invoice = InvoiceRecipe.make(created_by=contractor)
invoice.items.add(InvoiceItemRecipe.make())
invoice.items.add(InvoiceItemRecipe.make())