summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlejandro <tovarich@gmail.com>2016-01-06 19:36:10 -0300
committerAlejandro <tovarich@gmail.com>2016-01-06 19:39:02 -0300
commit5000ddaaef582e7504babf4f8163de13b93e7459 (patch)
tree43627c41e7b45471b3b6901635d20ee5c0e3fb34
parentbe85908f5205810083c524a25c7da565788f2c03 (diff)
downloadfactory-boy-5000ddaaef582e7504babf4f8163de13b93e7459.tar
factory-boy-5000ddaaef582e7504babf4f8163de13b93e7459.tar.gz
optional forced flush on SQLAlchemyModelFactory
fixes rbarrois/factory_boy#81
-rw-r--r--docs/orms.rst4
-rw-r--r--factory/alchemy.py3
2 files changed, 7 insertions, 0 deletions
diff --git a/docs/orms.rst b/docs/orms.rst
index 9b209bc..bd481bd 100644
--- a/docs/orms.rst
+++ b/docs/orms.rst
@@ -333,6 +333,10 @@ To work, this class needs an `SQLAlchemy`_ session object affected to the :attr:
SQLAlchemy session to use to communicate with the database when creating
an object through this :class:`SQLAlchemyModelFactory`.
+ .. attribute:: force_flush
+
+ Force a session flush() at the end of :func:`~factory.alchemy.SQLAlchemyModelFactory._create()`.
+
A (very) simple example:
.. code-block:: python
diff --git a/factory/alchemy.py b/factory/alchemy.py
index 20da6cf..a9aab23 100644
--- a/factory/alchemy.py
+++ b/factory/alchemy.py
@@ -27,6 +27,7 @@ class SQLAlchemyOptions(base.FactoryOptions):
def _build_default_options(self):
return super(SQLAlchemyOptions, self)._build_default_options() + [
base.OptionDefault('sqlalchemy_session', None, inherit=True),
+ base.OptionDefault('force_flush', False, inherit=True),
]
@@ -43,4 +44,6 @@ class SQLAlchemyModelFactory(base.Factory):
session = cls._meta.sqlalchemy_session
obj = model_class(*args, **kwargs)
session.add(obj)
+ if cls._meta.force_flush:
+ session.flush()
return obj