summaryrefslogtreecommitdiff
path: root/README.rst
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-07-05 12:20:48 +0200
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-07-05 12:20:48 +0200
commitc4e749bf464cfd9aced3adcf729052079bc25eaa (patch)
treeea985d970ea124bfdb698a84f93357a9553b5cb8 /README.rst
parent674878a940d1048e56ade5df08876ba963faeeb8 (diff)
downloadfactory-boy-c4e749bf464cfd9aced3adcf729052079bc25eaa.tar
factory-boy-c4e749bf464cfd9aced3adcf729052079bc25eaa.tar.gz
Add some doc on subfactories.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
Diffstat (limited to 'README.rst')
-rw-r--r--README.rst35
1 files changed, 35 insertions, 0 deletions
diff --git a/README.rst b/README.rst
index 2c4b15d..fded509 100644
--- a/README.rst
+++ b/README.rst
@@ -166,6 +166,14 @@ Sequences can be combined with lazy attributes::
UserFactory().email # => mark+0@example.com
+If you wish to use a custom method to set the initial ID for a sequence, you can override the ``_setup_next_sequence`` class method::
+
+ class MyFactory(factory.Factory):
+
+ @classmethod
+ def _setup_next_sequence(cls):
+ return cls.FACTORY_FOR.objects.values_list('id').order_by('-id')[0] + 1
+
Customizing creation
--------------------
@@ -183,3 +191,30 @@ Factory._prepare method::
if create:
user.save()
return user
+
+Subfactories
+------------
+
+If one of your factories has a field which is another factory, you can declare it as a ``SubFactory``. This allows to define attributes of that field when calling
+the global factory, using a simple syntax : ``field__attr=42`` will set the attribute ``attr`` of the ``SubFactory`` defined in ``field`` to 42::
+
+ class InnerFactory(factory.Factory):
+ foo = 'foo'
+ bar = factory.LazyAttribute(lambda o: foo * 2)
+
+ class ExternalFactory(factory.Factory):
+ inner = factory.SubFactory(InnerFactory, foo='bar')
+
+ >>> e = ExternalFactory()
+ >>> e.foo
+ 'bar'
+ >>> e.bar
+ 'barbar'
+
+ >>> e2 : ExternalFactory(inner__bar='baz')
+ >>> e2.foo
+ 'bar'
+ >>> e2.bar
+ 'baz'
+
+