diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-07 00:09:43 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-07 00:09:43 +0200 |
commit | 872718c1eb7865878ff17c8ce7dbeb004a089e40 (patch) | |
tree | 2b00752cbaa7dd7ce457f29354c2e7b728701ae9 /docs/recipes.rst | |
parent | 94a9b2cf9ddd7be181c984793a5404e2b95a779e (diff) | |
parent | 5836329889ac45034978c69b6a6f7de4b0b5b75d (diff) | |
download | factory-boy-872718c1eb7865878ff17c8ce7dbeb004a089e40.tar factory-boy-872718c1eb7865878ff17c8ce7dbeb004a089e40.tar.gz |
Merge pull request #285 from sampaccoud/feature/add-documentation-and-test-about-factory-parent-attribute
Add documentation and test for subfactory using "factory_parent" attribute
Diffstat (limited to 'docs/recipes.rst')
-rw-r--r-- | docs/recipes.rst | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/docs/recipes.rst b/docs/recipes.rst index a3df7be..fe18f50 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -327,7 +327,21 @@ Here, we want: country = factory.SubFactory(CountryFactory) owner = factory.SubFactory(UserFactory, country=factory.SelfAttribute('..country')) +If the value of a field on the child factory is indirectly derived from a field on the parent factory, you will need to use LazyAttribute and poke the "factory_parent" attribute. +This time, we want the company owner to live in a country neighboring the country of the company: + +.. code-block:: python + + class CompanyFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.Company + + name = "ACME, Inc." + country = factory.SubFactory(CountryFactory) + owner = factory.SubFactory(UserFactory, + country=factory.LazyAttribute(lambda o: get_random_neighbor(o.factory_parent.country))) + Custom manager methods ---------------------- |