Python 如何创建有多个孩子的工厂男孩

Python 如何创建有多个孩子的工厂男孩,python,django,Python,Django,我有两个: 我想创建一个国家,其中有两个具有不同属性的子项 例如,somefactory.create()createsfoodorry和foodorry有两个居民: name=Paul, country=foo, age=33 name=Jamse, country=foo, age=34 如何操作?首先写下CountryFactory和ResidentFactory(如FactoryBoy文档中所述)。然后写下你的函数: def create_country_with_residents(

我有两个:

我想创建一个
国家
,其中有两个具有不同属性的子项

例如,
somefactory.create()
creates
foodorry
foodorry
有两个居民:

name=Paul, country=foo, age=33
name=Jamse, country=foo, age=34

如何操作?

首先写下
CountryFactory
ResidentFactory
(如FactoryBoy文档中所述)。然后写下你的函数:

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
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