diff options
author | Robrecht De Rouck <Robrecht.De.Rouck@gmail.com> | 2013-12-25 01:46:22 +0100 |
---|---|---|
committer | Raphaƫl Barrois <raphael.barrois@polytechnique.org> | 2013-12-30 14:14:08 +0100 |
commit | c404da5080c92527e26c6b82d5cc5d7d88ba3d9a (patch) | |
tree | 8b9278b4d74ff705c26361187bdc494f57d0e2e4 /docs | |
parent | 874b5361d2972dd6feb2d26b74e37eba2434ba04 (diff) | |
download | factory-boy-c404da5080c92527e26c6b82d5cc5d7d88ba3d9a.tar factory-boy-c404da5080c92527e26c6b82d5cc5d7d88ba3d9a.tar.gz |
Document custom manager method recipe (Closes #119).
Diffstat (limited to 'docs')
-rw-r--r-- | docs/recipes.rst | 22 |
1 files changed, 22 insertions, 0 deletions
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() <django.db.models.query.QuerySet.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) |