From c404da5080c92527e26c6b82d5cc5d7d88ba3d9a Mon Sep 17 00:00:00 2001 From: Robrecht De Rouck Date: Wed, 25 Dec 2013 01:46:22 +0100 Subject: Document custom manager method recipe (Closes #119). --- docs/recipes.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'docs') diff --git a/docs/recipes.rst b/docs/recipes.rst index c1f3700..9e07413 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -291,3 +291,25 @@ Here, we want: name = "ACME, Inc." country = factory.SubFactory(CountryFactory) owner = factory.SubFactory(UserFactory, country=factory.SelfAttribute('..country')) + + +Custom manager methods +---------------------- + +Sometimes you need a factory to call a specific manager method other then the +default :meth:`Model.objects.create() ` method: + +.. code-block:: python + + class UserFactory(factory.DjangoModelFactory): + FACTORY_FOR = UserenaSignup + username = "l7d8s" + email = "my_name@example.com" + password = "my_password" + + @classmethod + def _create(cls, target_class, *args, **kwargs): + """Override the default ``_create`` with our custom call.""" + manager = cls._get_manager(target_class) + # The default would use ``manager.create(*args, **kwargs)`` + return manager.create_user(*args, **kwargs) -- cgit v1.2.3