diff options
author | Hervé Cauwelier <herve.cauwelier@polyconseil.fr> | 2016-02-12 17:31:04 +0100 |
---|---|---|
committer | Hervé Cauwelier <herve.cauwelier@polyconseil.fr> | 2016-02-12 17:31:04 +0100 |
commit | f2c075c40fd331b7d26a9db72aad249b2165eac4 (patch) | |
tree | 9a9de1a7c1a608b40b2ec18610edad361be28602 /docs/introduction.rst | |
parent | 38f4a69db8f71cb52b9e7fd8d6e20e7d052a5b8d (diff) | |
download | factory-boy-f2c075c40fd331b7d26a9db72aad249b2165eac4.tar factory-boy-f2c075c40fd331b7d26a9db72aad249b2165eac4.tar.gz |
factory: LazyFunction to just call a function in the simplest case
No need to wrap it in a lambda to strip the object argument from LazyAttribute or the sequence argument from Sequence.
Diffstat (limited to 'docs/introduction.rst')
-rw-r--r-- | docs/introduction.rst | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/docs/introduction.rst b/docs/introduction.rst index d00154d..9a16c39 100644 --- a/docs/introduction.rst +++ b/docs/introduction.rst @@ -117,6 +117,35 @@ This is achieved with the :class:`~factory.Sequence` declaration: return 'user%d' % n +LazyFunction +------------ + +In simple cases, calling a function is enough to compute the value. If that function doesn't depend on the object +being built, use :class:`~factory.LazyFunction` to call that function; it should receive a function taking no +argument and returning the value for the field: + +.. code-block:: python + + class LogFactory(factory.Factory): + class Meta: + model = models.Log + + timestamp = factory.LazyFunction(datetime.now) + +.. code-block:: pycon + + >>> LogFactory() + <Log: log at 2016-02-12 17:02:34> + + >>> # The LazyFunction can be overriden + >>> LogFactory(timestamp=now - timedelta(days=1)) + <Log: log at 2016-02-11 17:02:34> + + +.. note:: For complex cases when you happen to write a specific function, + the :meth:`~factory.@lazy_attribute` decorator should be more appropriate. + + LazyAttribute ------------- |