damd
damd

Reputation: 6997

Creating reverse relationship objects with Model Mommy

I use Model Mommy for creating test data and it's working fine so far.

Now I have a Django model named Invoice and a related model named InvoiceItem.

class Invoice(models.Model):
    created_by = models.ForeignKey(users.User)

class InvoiceItem(models.Model):
    invoice = models.ForeignKey(Invoice, related_name='items')

I've created two simple Mommy recipes for these models:

InvoiceRecipe = Recipe(
    Invoice,
    created_by=foreign_key(UserRecipe),
)

InvoiceItemRecipe = Recipe(
    InvoiceItem,
    invoice=foreign_key(InvoiceRecipe),
)

Now I'd like InvoiceRecipe.make() to automatically add a varying number of InvoiceItem objects to the invoice that is created.

Currently, I'm doing it like this, but I'd like it to automatically happen:

invoice = InvoiceRecipe.make(created_by=contractor)
invoice.items.add(InvoiceItemRecipe.make())
invoice.items.add(InvoiceItemRecipe.make())

Upvotes: 1

Views: 300

Answers (0)

Related Questions