How to create factory boy with several child
<p>I have two <a href="https://docs.djangoproject.com/en/1.10/topics/db/models/" rel="nofollow">Django models</a> like:  </p>

<pre><code>class Country(models.Model):
    country_name = models.CharField(max_length=10)

class Resident(models.Model):
    country = models.ForeignKey('Country')
    name = models.CharField(max_length=10)
    age = models.PositiveSmallInteger()
</code></pre>

<p>and I want to <a href="http://factoryboy.readthedocs.io/en/latest/orms.html" rel="nofollow">Factory Boy</a> create a <code>Country</code> with two children which have different attribute(s).</p>

<p>For example, <code>somefactory.create()</code> creates <code>FooCountry</code> and <code>FooCountry</code> has two residents: </p>

<pre><code>name=Paul, country=foo, age=33
name=Jamse, country=foo, age=34
</code></pre>

<p>How to do it?</p>

<p>First write down <code>CountryFactory</code> and <code>ResidentFactory</code> (as described in FactoryBoy documentation). Then write your function:</p>

<pre><code>def create_country_with_residents():
    country = CountryFactory.create()
    ResidentFactory.create(country=country, name=Paul, age=33)
    ResidentFactory.create(country=country, name=James, age=34)
    return country
</code></pre>

python django