ChangeLog
3.3.4 (unreleased)
Add support for Django 5.2
3.3.3 (2025-02-03)
New:
Publish type annotations
3.3.2 (2025-02-03)
Bugfix:
Fix docs generation
New:
Add support for Python 3.13
3.3.1 (2024-08-18)
New:
Add support for Django 4.2
Add support for Django 5.1
Add support for Python 3.12
issue 903: Add basic typing annotations
Run the test suite against
mongomockinstead of an actual MongoDB server
Bugfix:
issue 1031: Do not require
sqlalchemy_sessionwhensqlalchemy_session_factoryis provided.
Removed:
Stop advertising and verifying support for Django 3.2, 4.0, 4.1
3.3.0 (2023-07-19)
New:
issue 366: Add
factory.django.Passwordto generate DjangoUserpasswords.issue 304: Add
sqlalchemy_session_factoryto dynamically create sessions for use by theSQLAlchemyModelFactory.Add support for Django 4.0
Add support for Django 4.1
Add support for Python 3.10
Add support for Python 3.11
Bugfix:
Make
mute_signals()mute signals during post-generation.issue 775: Change the signature for
SQLAlchemyModelFactory’s_saveand_get_or_createmethods to avoid argument names clashes with a field namedsession.
Deprecated:
DjangoModelFactorywill stop issuing a second call tosave()on the created instance when Post-generation hooks return a value.To help with the transition,
factory.django.DjangoModelFactory’s_after_postgenerationraises aDeprecationWarningwhen callingsave(). Inspect yourDjangoModelFactorysubclasses:
If the
save()call is not needed afterPostGeneration, setfactory.django.DjangoOptions.skip_postgeneration_savetoTruein the factory meta.Otherwise, the instance has been modified by
PostGenerationhooks and needs to besave()d. Either:
call
django.db.models.Model.save()in thePostGenerationhook that modifies the instance, oroverride the
_after_postgenerationmethod tosave()the instance.
Removed:
Drop support for Django 2.2
Drop support for Django 3.0
Drop support for Django 3.1
Drop support for Python 3.6
Drop support for Python 3.7
3.2.1 (2021-10-26)
- New:
Add support for Django 3.2
Bugfix:
Do not override signals receivers registered in a
mute_signals()context.issue 775: Change the signature for
SQLAlchemyModelFactory’s_saveand_get_or_createmethods to avoid argument names clashes with a field namedsession.
3.2.0 (2020-12-28)
New:
Add support for Django 3.1
Add support for Python 3.9
Removed:
Drop support for Django 1.11. This version is not maintained anymore.
Drop support for Python 3.5. This version is not maintained anymore.
Deprecated:
factory.use_strategy(). Usefactory.FactoryOptions.strategyinstead. The purpose ofuse_strategy()duplicates the factory option. Follow PEP 20: There should be one– and preferably only one –obvious way to do it.
use_strategy()will be removed in the next major version.
Bug fix:
issue 785 issue 786 issue 787 issue 788 issue 790 issue 796: Calls to
factory.Fakerandfactory.django.FileFieldwithin aTraitorMaybeno longer lead to aKeyErrorcrash.
3.1.0 (2020-10-02)
New:
Allow all types of declarations in
factory.Fakercalls - enables references to other faker-defined attributes.
3.0.1 (2020-08-13)
Bug fix:
issue 769: Fix
import factory; factory.django.DjangoModelFactoryand similar calls.
3.0.0 (2020-08-12)
Breaking changes
The following aliases were removed:
+================================================+===================================================+
| Broken alias | New import |
+================================================+===================================================+
| from factory import DjangoModelFactory | from factory.django import DjangoModelFactory |
+————————————————+—————————————————+
| from factory import MogoFactory | from factory.mogo import MogoFactory |
+————————————————+—————————————————+
| from factory.fuzzy import get_random_state | from factory.random import get_random_state |
+————————————————+—————————————————+
| from factory.fuzzy import set_random_state | from factory.random import set_random_state |
+————————————————+—————————————————+
| from factory.fuzzy import reseed_random | from factory.random import reseed_random |
+================================================+===================================================+
Removed:
Drop support for Python 2 and 3.4. These versions are not maintained anymore.
Drop support for Django 2.0 and 2.1. These versions are not maintained anymore.
Remove deprecated
force_flushfromSQLAlchemyModelFactoryoptions. Usesqlalchemy_session_persistence = "flush"instead.Drop deprecated
attributes()fromFactorysubclasses; usefactory.make_factory(dict, FactoryClass._meta.pre_declarations)instead.Drop deprecated
declarations()fromFactorysubclasses; useFactoryClass._meta.pre_declarationsinstead.Drop
factory.compatmodule.
New:
Add support for Python 3.8
Add support for Django 2.2 and 3.0
Report misconfiguration when a
Factoryis used as themodelfor anotherFactory.Allow configuring the color palette of
ImageField.
get_random_state()now represents the state of Faker andfactory_boyfuzzy attributes.Add SQLAlchemy
get_or_createsupport
Improvements:
issue 561: Display a developer-friendly error message when providing a model instead of a factory in a
SubFactoryclass.
Bug fix:
Fix issue with SubFactory not preserving signal muting behavior of the used factory, thanks Patrick Stein.
Fix issue with overriding parameters in a Trait, thanks Grégoire Rocher.
issue 598: Limit
get_or_createbehavior to fields specified indjango_get_or_create.issue 606: Re-raise
IntegrityErrorwhendjango_get_or_createwith multiple fields fails to lookup model using user provided keyword arguments.issue 630: TypeError masked by __repr__ AttributeError when initializing
Maybewith inconsistent phases.
2.12.0 (2019-05-11)
New:
Add support for Python 3.7
Add support for Django 2.1
Add
gettertoFuzzyChoicethat mimics the behavior ofgetterinIteratorMake the
extra_kwargsparameter ofFaker’sgeneratemethod optionalAdd
RelatedFactoryListclass for one-to-many support, thanks Sean Harrington.Make the locale argument for
Fakerkeyword-only
Bug fix:
Allow renamed arguments to be optional, thanks to Justin Crown.
Fix django_get_or_create behavior when using multiple fields with unique=True, thanks to @YPCrumble <https://github.com/YPCrumble>
2.11.1 (2018-05-05)
Bug fix:
Fix passing deep context to a
SubFactory(Foo(x__y__z=factory.Faker('name'))
2.11.0 (2018-05-05)
Bug fix:
Fix
FuzzyFloatto return a 15 decimal digits precision float by defaultissue 451: Restore
FileFieldto afactory.declarations.ParameteredAttribute, relying on composition to parse the provided parameters.issue 389: Fix random state management with
faker.issue 466: Restore mixing
Traitandpost_generation().
2.10.0 (2018-01-28)
Bug fix:
issue 443: Don’t crash when calling
factory.Iterator.reset()on a brand new iterator.
New:
issue 397: Allow a
factory.Maybeto contain aPostGenerationdeclaration. This also applies tofactory.Trait, since they use afactory.Maybedeclaration internally.
2.9.2 (2017-08-03)
Bug fix:
Fix declaration corruption bug when a factory defined foo__bar__baz=1 and a caller provided a foo__bar=x parameter at call time: this got merged into the factory’s base declarations.
2.9.1 (2017-08-02)
Bug fix:
Fix packaging issues (see https://github.com/zestsoftware/zest.releaser/issues/212)
Don’t crash when debugging PostGenerationDeclaration
2.9.0 (2017-07-30)
This version brings massive changes to the core engine, thus reducing the number of corner cases and weird behaviors.
New:
issue 275: factory.fuzzy and factory.faker now use the same random seed.
Add
factory.Maybe, which chooses among two possible declarations based on another field’s value (powers theTraitfeature).
PostGenerationMethodCallonly allows to pass one positional argument; use keyword arguments for extra parameters.
Deprecation:
factory.fuzzy.get_random_state is deprecated, factory.random.get_random_state should be used instead.
factory.fuzzy.set_random_state is deprecated, factory.random.set_random_state should be used instead.
factory.fuzzy.reseed_random is deprecated, factory.random.reseed_random should be used instead.
2.8.1 (2016-12-17)
Bug fix:
Fix packaging issues.
2.8.0 (2016-12-17)
New:
issue 240: Call post-generation declarations in the order they were declared, thanks to Oleg Pidsadnyi.
issue 309: Provide new options for SQLAlchemy session persistence
Bug fix:
issue 334: Adjust for the package change in
faker
2.7.0 (2016-04-19)
New:
pull request 267: Add
factory.LazyFunctionto remove unneeded lambda parameters, thanks to Hervé Cauwelier.issue 251: Add parameterized factories and
traitspull request 256, pull request 292: Improve error messages in corner cases
Removed:
pull request 278: Formally drop support for Python2.6
Warning
Version 2.7.0 moves all error classes to factory.errors. This breaks existing import statements for any error classes except those importing FactoryError directly from the factory module.
2.6.1 (2016-02-10)
New:
pull request 262: Allow optional forced flush on SQLAlchemy, courtesy of Minjung.
2.6.0 (2015-10-20)
New:
Add
factory.FactoryOptions.renameto help handle conflicting names (issue 206)Add support for random-yet-realistic values through fake-factory, through the
factory.Fakerclass.
factory.Iteratorno longer begins iteration of its argument at import time, thus allowing to pass in a lazy iterator such as a Django queryset (i.efactory.Iterator(models.MyThingy.objects.all())).Simplify imports for ORM layers, now available through a simple
factoryimport, atfactory.alchemy.SQLAlchemyModelFactory/factory.django.DjangoModelFactory/factory.mongoengine.MongoEngineFactory.
Bug fix:
issue 201: Properly handle custom Django managers when dealing with abstract Django models.
issue 212: Fix
factory.django.mute_signals()to handle Django’s signal cachingissue 228: Don’t load
django.apps.apps.get_model()until requiredpull request 219: Stop using
mogo.model.Model.new(), deprecated 4 years ago.
2.5.2 (2015-04-21)
Bug fix:
Add support for Django 1.7/1.8
Add support for mongoengine>=0.9.0 / pymongo>=2.1
2.5.1 (2015-03-27)
Bug fix:
Respect custom managers in
DjangoModelFactory(see issue 192)Allow passing declarations (e.g
Sequence) as parameters toFileFieldandImageField.
2.5.0 (2015-03-26)
New:
Add support for getting/setting
factory.fuzzy’s random state (see issue 175, issue 185).Support lazy evaluation of iterables in
factory.fuzzy.FuzzyChoice(see issue 184).Support non-default databases at the factory level (see issue 171)
Make
factory.django.FileFieldandfactory.django.ImageFieldnon-post_generation, i.e normal fields also available insave()(see issue 141).
Bug fix:
Avoid issues when using
factory.django.mute_signals()on a base factory class (see issue 183).Fix limitations of
factory.StubFactory, that can now usefactory.SubFactoryand co (see issue 131).
Deprecation:
Remove deprecated features from 2.4.0 (2014-06-21)
Remove the auto-magical sequence setup (based on the latest primary key value in the database) for Django and SQLAlchemy; this relates to issues issue 170, issue 153, issue 111, issue 103, issue 92, issue 78. See https://github.com/FactoryBoy/factory_boy/commit/13d310f for technical details.
Warning
Version 2.5.0 removes the ‘auto-magical sequence setup’ bug-and-feature. This could trigger some bugs when tests expected a non-zero sequence reference.
Upgrading
Warning
Version 2.5.0 removes features that were marked as deprecated in v2.4.0.
All FACTORY_*-style attributes are now declared in a class Meta: section:
# Old-style, deprecated
class MyFactory(factory.Factory):
FACTORY_FOR = models.MyModel
FACTORY_HIDDEN_ARGS = ['a', 'b', 'c']
# New-style
class MyFactory(factory.Factory):
class Meta:
model = models.MyModel
exclude = ['a', 'b', 'c']
A simple shell command to upgrade the code would be:
# sed -i: inplace update
# grep -l: only file names, not matching lines
sed -i 's/FACTORY_FOR =/class Meta:\n model =/' $(grep -l FACTORY_FOR $(find . -name '*.py'))
This takes care of all FACTORY_FOR occurrences; the files containing other attributes to rename can be found with grep -R FACTORY .
2.4.1 (2014-06-23)
Bug fix:
Fix overriding deeply inherited attributes (set in one factory, overridden in a subclass, used in a sub-sub-class).
2.4.0 (2014-06-21)
New:
Add support for
factory.fuzzy.FuzzyInteger.step, thanks to ilya-pirogov (pull request 120)Add
mute_signals()decorator to temporarily disable some signals, thanks to ilya-pirogov (pull request 122)Add
FuzzyFloat(issue 124)Declare target model and other non-declaration fields in a
class Metasection.
Deprecation:
Use of
FACTORY_FORand otherFACTORYclass-level attributes is deprecated and will be removed in 2.5. Those attributes should now declared within theclass Metaattribute:For
factory.Factory:
Rename
factory.Factory.FACTORY_FORtomodelRename
factory.Factory.ABSTRACT_FACTORYtoabstractRename
factory.Factory.FACTORY_STRATEGYtostrategyRename
factory.Factory.FACTORY_ARG_PARAMETERStoinline_argsRename
factory.Factory.FACTORY_HIDDEN_ARGStoexcludeFor
factory.django.DjangoModelFactory:
Rename
factory.django.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATEtodjango_get_or_createFor
factory.alchemy.SQLAlchemyModelFactory:
Rename
factory.alchemy.SQLAlchemyModelFactory.FACTORY_SESSIONtosqlalchemy_session
2.3.1 (2014-01-22)
Bug fix:
Fix badly written assert containing state-changing code, spotted by
chsigi(pull request 126)Don’t crash when handling objects whose
__repr__is non-pure-ASCII bytes on Python 2, discovered by mbertheau (issue 123) and strycore (pull request 127)
2.3.0 (2013-12-25)
New:
Add
FuzzyText, thanks to jdufresne (pull request 97)Add
FuzzyDecimal, thanks to thedrow (pull request 94)Add support for
EmbeddedDocument, thanks to imiric (pull request 100)
2.2.1 (2013-09-24)
Bug fix:
Fixed sequence counter for
DjangoModelFactorywhen a factory inherits from another factory relating to an abstract model.
2.2.0 (2013-09-24)
Bug fix:
Removed duplicated
SQLAlchemyModelFactorylurking infactory(pull request 83)Properly handle sequences within object inheritance chains. If
FactoryAinherits fromFactoryB, and their associated classes share the same link, sequence counters will be shared (issue 93)Properly handle nested
SubFactoryoverrides
New:
The
DjangoModelFactorynow supports theFACTORY_FOR = 'myapp.MyModel'syntax, making it easier to shove all factories in a single module (issue 66).Add
factory.debug()helper for easier backtrace analysisAdding factory support for mongoengine with
MongoEngineFactory.
2.1.2 (2013-08-14)
New:
2.1.1 (2013-07-02)
Bug fix:
Properly retrieve the
colorkeyword argument passed toImageField
2.1.0 (2013-06-26)
New:
Add
FuzzyDatethanks to saulshanabrookAdd
FuzzyDateTimeandFuzzyNaiveDateTime.Add a
factory_parentattribute to thefactory.builder.Resolverpassed toLazyAttribute, in order to access fields defined in wrapping factories.Move
DjangoModelFactoryandMogoFactoryto their own modules (factory.djangoandfactory.mogo)Add the
reset_sequence()classmethod toFactoryto ease resetting the sequence counter for a given factory.Add debug messages to
factorylogger.Add support for the SQLAlchemy ORM through
SQLAlchemyModelFactory(pull request 64, thanks to Romain Commandé)Add
factory.django.FileFieldandfactory.django.ImageFieldhooks for related Django model fields (issue 52)
Bug fix
Properly handle non-integer primary keys in
DjangoModelFactory(issue 57).Disable
RelatedFactorygeneration when a specific value was passed (issue 62, thanks to Gabe Koscky)
Deprecation:
Rename
RelatedFactory’snameargument tofactory_related_name(See issue 58)
2.0.2 (2013-04-16)
New:
When
factory.django.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATEis empty, useModel.objects.create()instead ofModel.objects.get_or_create.
2.0.1 (2013-04-16)
New:
Don’t push
defaultstoget_or_createwhenfactory.django.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATEis not set.
2.0.0 (2013-04-15)
New:
Allow overriding the base factory class for
make_factory()and friends.Fields listed in
factory.Factory.FACTORY_HIDDEN_ARGSwon’t be passed to the associated class’ constructorAdd support for
get_or_createinDjangoModelFactory, throughfactory.django.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATE.Add support for
fuzzyattribute definitions.The
Sequencecounter can be overridden when calling a generating function
Removed:
Remove associated class discovery
Remove
factory.InfiniteIteratorandfactory.infinite_iteratorRemove
factory.CircularSubFactoryRemove
extract_prefixkwarg to post-generation hooks.Stop defaulting to Django’s
Foo.objects.create()when “creating” instancesRemove STRATEGY_*
Remove
factory.Factory.set_building_function/factory.Factory.set_creation_function
1.3.0 (2013-03-11)
Warning
This version deprecates many magic or unexplicit features that will be removed in v2.0.0.
Please read the Upgrading section, then run your
tests with python -W default to see all remaining warnings.
New
- Global:
Rewrite the whole documentation
Provide a dedicated
MogoFactorysubclass ofFactory
- The Factory class:
Better creation/building customization hooks at
factory.Factory._build()andfactory.Factory.create()Add support for passing non-kwarg parameters to a
Factorywrapped class throughFACTORY_ARG_PARAMETERS.Keep the
FACTORY_FORattribute inFactoryclasses
- Declarations:
Allow
SubFactoryto solve circular dependencies between factoriesEnhance
SelfAttributeto handle “container” attribute fetchingA
Iteratormay be prevented from cycling by setting itscycleargument toFalseAllow overriding default arguments in a
PostGenerationMethodCallwhen generating an instance of the factoryAn object created by a
DjangoModelFactorywill be saved again afterPostGenerationhooks execution
Pending deprecation
The following features have been deprecated and will be removed in an upcoming release.
- Declarations:
factory.InfiniteIteratoris deprecated in favor ofIteratorfactory.CircularSubFactoryis deprecated in favor ofSubFactoryThe
extract_prefixargument topost_generation()is now deprecated
- Factory:
Usage of
factory.Factory.set_creation_functionandfactory.Factory.set_building_functionare now deprecatedImplicit associated class discovery is no longer supported, you must set the
FACTORY_FORattribute on allFactorysubclasses
Upgrading
This version deprecates a few magic or undocumented features. All warnings will turn into errors starting from v2.0.0.
In order to upgrade client code, apply the following rules:
Add a
FACTORY_FORattribute pointing to the target class to eachFactory, instead of relying on automatic associated class discoveryWhen using factory_boy for Django models, have each factory inherit from
DjangoModelFactoryReplace
factory.CircularSubFactory('some.module', 'Symbol')withfactory.SubFactory('some.module.Symbol')Replace
factory.InfiniteIterator(iterable)withfactory.Iterator(iterable)Replace
@factory.post_generation()with@factory.post_generationReplace
factory.set_building_function(SomeFactory, building_function)with an override of the_build()method ofSomeFactoryReplace
factory.set_creation_function(SomeFactory, creation_function)with an override of the_create()method ofSomeFactory
1.2.0 (2012-09-08)
New:
Add
factory.CircularSubFactoryto solve circular dependencies between factories
1.1.5 (2012-07-09)
Bug fix:
Fix
factory.PostGenerationDeclarationand derived classes.
1.1.4 (2012-06-19)
New:
Add
use_strategy()decorator to override aFactory’s default strategyImprove test running (tox, python2.6/2.7)
Introduce
PostGenerationandRelatedFactory
1.1.3 (2012-03-09)
Bug fix:
Fix packaging rules
1.1.2 (2012-02-25)
New:
Add
Iteratorandfactory.InfiniteIteratorforFactoryattribute declarations.Provide
generate()andsimple_generate(), that allow specifying the instantiation strategy directly. Also providesgenerate_batch()andsimple_generate_batch().
1.1.1 (2012-02-24)
New:
Add
build_batch(),create_batch()andstub_batch(), to instantiate factories in batch
1.1.0 (2012-02-24)
New:
Improve the
SelfAttributesyntax to fetch sub-attributes using thefoo.barsyntax;Add
factory.ContainerAttributeto fetch attributes from the container of aSubFactory.Provide the
make_factory()helper:MyClassFactory = make_factory(MyClass, x=3, y=4)
Bug fix:
Allow
classmethod/staticmethodon factories
Deprecation:
Auto-discovery of
factory.Factory.FACTORY_FORbased on class name is now deprecated
1.0.4 (2011-12-21)
New:
Improve the algorithm for populating a
Factoryattributes dictAdd
python setup.py testcommand to run the test suiteAllow custom build functions
Introduce
factory.MOGO_BUILDbuild functionAdd support for inheriting from multiple
FactoryBase
Factoryclasses can now be declared abstract throughfactory.Factory.ABSTRACT_FACTORY.Provide
DjangoModelFactory, whoseSequencecounter starts at the next free database idIntroduce
SelfAttribute, a shortcut forfactory.LazyAttribute(lambda o: o.foo.bar.baz.
Bug fix:
Handle nested
SubFactoryShare sequence counter between parent and subclasses
Fix
SubFactory/Sequenceinterference
1.0.2 (2011-05-16)
New:
Introduce
SubFactory
1.0.1 (2011-05-13)
New:
Allow
FactoryinheritanceImprove handling of custom build/create functions
Bug fix:
Fix concurrency between
LazyAttributeandSequence
1.0.0 (2010-08-22)
New:
First version of factory_boy
Credits
See Credits.