Search code examples
djangofakerfactory-boy

Django Factory Boy and Faker always return same value


I'm trying to generate dummy data and I have a selectable field among several options but the same sector is always generated

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)

Solution

  • Updated with the correct code

    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)