Model Mommy: Multiple recipes with foreign key relation to a single recipe
<p>I have had this annoyance with ModelMommy for a while but I can't figure out how to do this properly.</p>

<p>Let`s assume a simple relation:</p>

<pre><code>class Organization(models.Model):
    label = models.CharField(unique=True)

class Asset(models.Model):
    organization = models.ForeignKey(Organization)
    label = models.CharField(unique=True)
</code></pre>

<p>And recipes:</p>

<pre><code>from model_mommy.recipe import Recipe, foreign_key


organization_recipe = Recipe(Organization, label='My Organization') 
asset1_recipe = Recipe(Asset,
                      organization=foreign_key(organization_recipe),
                      label='asset 1')
asset2_recipe = Recipe(Asset,
                      organization=foreign_key(organization_recipe),
                      label='asset 2')
</code></pre>

<p>Now when I make these asset recipes I get an error:</p>

<pre><code>&gt;&gt; asset1 = asset1_recipe.make()
&gt;&gt; asset2 = asset2_recipe.make()
IntegrityError: duplicate key value violates unique constraint "organizations_organization_label_key"
DETAIL:  Key (label)=(My Organization) already exists.
</code></pre>

<p>This can be solved by providing asset1's organization as a parameter into asset2's make method:</p>

<pre><code>&gt;&gt; asset1 = asset1_recipe.make()
&gt;&gt; asset2 = asset2_recipe.make(organization=asset1.organization)
</code></pre>

<p>But there has to be a simpler, more clean way of doing this.</p>

<p><strong>EDIT</strong></p>

<p>Based on the link in Helgi's answer I've changed all my recipe foreign keys to point to a closure:</p>

<pre><code>def organization_get_or_create(**kwargs):
    """
    Returns a closure with details of the organization to be fetched from db or
    created. Must be a closure to ensure it's executed within a test case
    Parameters
    ----------
    kwargs
        the details of the desired organization
    Returns
    -------
    Closure
        which returns the desired organization
    """

    def get_org():
        org, new = models.Organization.objects.get_or_create(**kwargs)
        return org

    return get_org

my_org = organization_get_or_create(label='My Organization')

asset1_recipe = Recipe(Asset,
                      organization=my_org,
                      label='asset 1')
asset2_recipe = Recipe(Asset,
                      organization=my_org,
                      label='asset 2')
</code></pre>

<p>And can create as many assets as I like:</p>

<pre><code>&gt;&gt; asset1 = asset1_recipe.make()
&gt;&gt; asset2 = asset2_recipe.make()
</code></pre>