From c4e749bf464cfd9aced3adcf729052079bc25eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Tue, 5 Jul 2011 12:20:48 +0200 Subject: Add some doc on subfactories. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Barrois --- README.rst | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'README.rst') 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' + + -- cgit v1.2.3