Django Factory Boy 和 Faker 总是 return 相同的值
Django Factory Boy and Faker always return same value
我正在尝试生成虚拟数据,我在多个选项中有一个可选字段,但始终生成相同的扇区
models.py:
SECTOR = (("1", _("Administración y gestión")), ("2", _("Agricultura y ganadería")), ("3", _("Industria alimentaria")), ("4", _("Grandes almacenes")), ("5", _("Comercio")), ("6", _("Construcción e industrias extractivas")), ("7", _("Actividades físico-deportivas")))
class Company(TimeEntity):
...
sector = models.CharField(verbose_name=_("Sector"), max_length=20, choices=SECTOR, default=1)
factories.py:
SECTOR_FACTORY = list(map(lambda x: x[0], app_models.SECTOR))
class CompanyFactory(DjangoModelFactory):
class Meta:
model = app_models.Company
sector = fake.random_choices(elements=SECTOR_FACTORY, length=1)
更新了正确的代码
from factory.fuzzy import FuzzyChoice
SECTOR_FACTORY = list(map(lambda x: x[0], app_models.SECTOR))
class CompanyFactory(DjangoModelFactory):
class Meta:
model = app_models.Company
sector = FuzzyChoice(SECTOR_FACTORY)
我正在尝试生成虚拟数据,我在多个选项中有一个可选字段,但始终生成相同的扇区
models.py:
SECTOR = (("1", _("Administración y gestión")), ("2", _("Agricultura y ganadería")), ("3", _("Industria alimentaria")), ("4", _("Grandes almacenes")), ("5", _("Comercio")), ("6", _("Construcción e industrias extractivas")), ("7", _("Actividades físico-deportivas")))
class Company(TimeEntity):
...
sector = models.CharField(verbose_name=_("Sector"), max_length=20, choices=SECTOR, default=1)
factories.py:
SECTOR_FACTORY = list(map(lambda x: x[0], app_models.SECTOR))
class CompanyFactory(DjangoModelFactory):
class Meta:
model = app_models.Company
sector = fake.random_choices(elements=SECTOR_FACTORY, length=1)
更新了正确的代码
from factory.fuzzy import FuzzyChoice
SECTOR_FACTORY = list(map(lambda x: x[0], app_models.SECTOR))
class CompanyFactory(DjangoModelFactory):
class Meta:
model = app_models.Company
sector = FuzzyChoice(SECTOR_FACTORY)