如何创造几个孩子的工厂男孩

2024-03-29 01:42:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个Django models像:

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

我想Factory Boy创建一个Country,其中有两个具有不同属性的子级。你知道吗

例如,somefactory.create()创建FooCountryFooCountry有两个驻留:

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

怎么做?你知道吗


Tags: djangonameagemodelfoomodelscountrylength
1条回答
网友
1楼 · 发布于 2024-03-29 01:42:10

首先写下CountryFactoryResidentFactory(如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

相关问题 更多 >