Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
fix nested m2m recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
vandersonmota committed Mar 27, 2016
1 parent 37a47cc commit 1e46b6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions model_mommy/recipe.py
Expand Up @@ -55,7 +55,7 @@ def _mapping(self, new_attrs):
recipe_attrs = mommy.filter_rel_attrs(k, **a)
mapping[k] = v.recipe.make(**recipe_attrs)
elif isinstance(v, related):
mapping[k] = v.prepare()
mapping[k] = v.make()
mapping.update(new_attrs)
mapping.update(rel_fields_attrs)
return mapping
Expand Down Expand Up @@ -151,10 +151,9 @@ def __init__(self, *args):
else:
raise TypeError('Not a recipe')

def prepare(self):
def make(self):
"""
Django related manager saves related set.
No need to persist at first
Persists objects to m2m relation
"""
return [m.prepare() for m in self.related]
return [m.make() for m in self.related]

4 changes: 4 additions & 0 deletions test/generic/mommy_recipes.py
Expand Up @@ -59,6 +59,10 @@
friends_with=related(dog, dog),
)

dog_with_more_friends = dog.extend(
friends_with=related(dog_with_friends),
)

extended_dog = dog.extend(
breed = 'Super basset',
)
Expand Down
6 changes: 6 additions & 0 deletions test/generic/tests/test_recipes.py
Expand Up @@ -353,6 +353,12 @@ def test_create_many_to_many(self):
self.assertEqual(friend.breed, 'Pug')
self.assertEqual(friend.owner.name, 'John Doe')

def test_create_nested(self):
dog = mommy.make_recipe('test.generic.dog_with_more_friends')
self.assertEqual(len(dog.friends_with.all()), 1)
friend = dog.friends_with.all()[0]
self.assertEqual(len(friend.friends_with.all()), 2)


class TestSequences(TestCase):
def test_increment_for_strings(self):
Expand Down

0 comments on commit 1e46b6a

Please sign in to comment.