Package: autofactory
AutoFactoryBoyWarning!AutoFactoryBoysupports onlyDjangoORM for now.AutoFactoryBoyintrospects ORM models and generates factories.ContentsInstallationQuickstartCompatibilityQ & AContributingLicenseAcknowledgmentsInstallationInstall from PyPI:$pipinstallautofactoryBuild from source:$gitclonegit://github.com/nickgashkov/autofactoryboy/
$pythonsetup.pyinstallQuickstartThere are a couple of options to create anAutoFactoryfor a model:Subclass aDjangoModelAutoFactory:fromautofactory.djangoimportDjangoModelAutoFactoryfrommodelsimportModelclassModelFactory(DjangoModelAutoFactory):classMeta:model=Modelautofields="__all__"model=ModelFactory.create(some__field__to__change=42)Make a factory right from the model with the help of a
shortcut:fromautofactory.djangoimportautofactoryfrommodelsimportModelmodel_factory=autofactory(Model)model=model_factory.create(some__field__to__change=42)CompatibilityPythonDjangoSQLAlchemyMogomongoengine2.71.11———3.41.11, 2.0———3.51.11, 2.0, 2.1———3.61.11, 2.0, 2.1———3.71.11, 2.0, 2.1———Q & AHow do I make an autofactory with specific fields?AutoFactoryBoywill generate aModelFactoryfor a model with fields,
declared in theModelFactory.Meta:classModelFactory(DjangoModelAutoFactory):classMeta:model=Modelautofields=("integer","string")The code snippet above is identical to:classModelFactory(DjangoModelFactory):integer=factory.Faker("pyint")string=factory.Faker("text")classMeta:model=ModelHow do I make an autofactory with all model fields?You can setfieldsto a special value (i.e.__all__) and all fields withblank=Falseand withoutdefaultwill be generated automatically:# models.pyclassModel(models.Model):integer=models.IntegerField(blank=True,null=True)text=models.TextField(default="Default")string=models.CharField(max_length=20)# factories.pyclassModelFactory(DjangoModelAutoFactory):classMeta:model=Modelautofields="__all__"The code snippet above is identical to:classModelFactory(DjangoModelFactory):string=factory.Faker("text",max_nb_chars=20)classMeta:model=ModelHow do I make an autofactory with all model fields except oneYou can add the field you want to exclude to theMeta.autoexcludetuple:# models.pyclassModel(models.Model):field=models.IntegerField(blank=False,null=True)field_to_exclude=models.IntegerField(blank=False,null=True)# factories.pyclassModelFactory(DjangoModelAutoFactory):classMeta:model=Modelautoexclude=("field_to_exclude",)The code snippet above is identical to:classModelFactory(DjangoModelFactory):field=factory.Faker("pyint")classMeta:model=ModelWarning!One cannot setautofieldsandautoexcludefor one factory at
the same time.How do I teach AutoFactoryBoy how to generate my custom fieldMake a custom builder and register it with decorator or as a function:# models.pyclassModel:custom=CustomField()# builders.pyfromautofactory.django.buildersimportregistry@registry.register(CustomField)defbuild_custom_field(field_cls):...registry.register(CustomField,build_custom_field)Warning!Order is important. Make sure, that you register all
custom fieldsbeforeany factory declaration. I.e.:fromautofactory.django.buildersimportregistry,FROM_DEFAULTfromautofactory.djangoimportautofactory,DjangoModelAutoFactoryfrommodelsimportModel# Register first.registry.register(FROM_DEFAULT,lambdax:"Default for everything")# Declare second.classModelFactory(DjangoModelAutoFactory):classMeta:model=Modelautofields="__all__"model_factory=autofactory(Model)How do I override AutoFactoryBoy field builderautofactory.django.builders.registryfor the rescue! Using the
approach above, you can redeclare builder for any field:fromautofactory.django.buildersimportregistryfromdjango.dbimportmodels@registry.register(models.CharField)defcustom_char_field_builder(field_cls):...ContributingDependenciesTo install dev dependencies, run:$pipinstallpip-tools
$makeupgradeCode formattingTo format the code, run:$makeTestingTo test, run:$maketest# Current environment$maketest-tox# All tox environmentsLicenseThis project is licensed under the MIT License — see theLICENSEfile for details.Acknowledgmentsfactory_boy