Skip to content

Faker custom color field

Requirement

  • factory_boy
py
from faker.providers import BaseProvider
from faker import Faker as BaseFaker


class FakerColorProvider(BaseProvider):
    __provider__ = "hex_color"

    def hex_color(self):
        return "#%06x" % random.randint(0, 0xFFFFFF)


our_faker = BaseFaker()
our_faker.add_provider(FakerColorProvider)


# Usage 
class EntityFactoryMixin(DjangoModelFactory):
    class Meta:
        model = EntityModel

    code = FactorySequence(lambda n: f"ent_{n}")
    name = FactorySequence(lambda n: f"ent_{n}")
    description = Faker("text")
    color = our_faker.hex_color() # our custonm color faked field

References