summaryrefslogtreecommitdiff
path: root/docs/reference.rst
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2016-02-14 23:12:02 +0000
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2016-02-14 23:12:02 +0000
commit4fc9caa17bd464cf749bfba6af0a4c698da30ccb (patch)
tree6ec891d81d4f1a363ad9ca47cc62ae19946d6985 /docs/reference.rst
parent86dc8baea87c69e1d39a3d87e1271fbd0e85ca63 (diff)
parentf2c075c40fd331b7d26a9db72aad249b2165eac4 (diff)
downloadfactory-boy-4fc9caa17bd464cf749bfba6af0a4c698da30ccb.tar
factory-boy-4fc9caa17bd464cf749bfba6af0a4c698da30ccb.tar.gz
Merge pull request #267 from bors-ltd/master
factory: LazyFunction to just call a function in the simplest case
Diffstat (limited to 'docs/reference.rst')
-rw-r--r--docs/reference.rst40
1 files changed, 38 insertions, 2 deletions
diff --git a/docs/reference.rst b/docs/reference.rst
index b5ccd16..9e01213 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -90,7 +90,7 @@ The :class:`Factory` class
model = Order
exclude = ('now',)
- now = factory.LazyAttribute(lambda o: datetime.datetime.utcnow())
+ now = factory.LazyFunction(datetime.datetime.utcnow)
started_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(hours=1))
paid_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(minutes=50))
@@ -551,6 +551,42 @@ Faker
smiley = factory.Faker('smiley')
+LazyFunction
+""""""""""""
+
+.. class:: LazyFunction(method_to_call)
+
+The :class:`LazyFunction` is the simplest case where the value of an attribute
+does not depend on the object being built.
+
+It takes as argument a method to call (function, lambda...); that method should
+not take any argument, though keyword arguments are safe but unused,
+and return a value.
+
+.. 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>
+
+Decorator
+~~~~~~~~~
+
+The class :class:`LazyFunction` does not provide a decorator.
+
+For complex cases, use :meth:`LazyAttribute.lazy_attribute` directly.
+
LazyAttribute
"""""""""""""
@@ -1041,7 +1077,7 @@ gains an "upward" semantic through the double-dot notation, as used in Python im
>>> company.owner.language
'fr'
-Obviously, this "follow parents" hability also handles overriding some attributes on call:
+Obviously, this "follow parents" ability also handles overriding some attributes on call:
.. code-block:: pycon