From 64a059101d0119e18c1cb0b15b0e9e15795ccba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Fri, 13 May 2011 16:06:13 +0200 Subject: Improve handling of custom build/create functions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only public attributes (i.e not starting with _) will be handled properly. Signed-off-by: Raphaƫl Barrois --- README.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'README.rst') diff --git a/README.rst b/README.rst index cb73a17..e141b93 100644 --- a/README.rst +++ b/README.rst @@ -165,3 +165,21 @@ Sequences can be combined with lazy attributes:: email = factory.LazyAttributeSequence(lambda a, n: '{0}+{1}@example.com'.format(a.name, n).lower()) UserFactory().email # => mark+0@example.com + +Customizing creation +-------------------- + +Sometimes, the default build/create by keyword arguments doesn't allow for enough +customization of the generated objects. In such cases, you should override the +:meth:`base.Factory._prepare` method:: + + class UserFactory(factory.Factory): + @classmethod + def _prepare(cls, create, **kwargs): + password = kwargs.pop('password', None) + user = super(UserFactory, cls)._prepare(create, kwargs) + if password: + user.set_password(user) + if create: + user.save() + return user -- cgit v1.2.3