Closed diegopau closed 9 years ago
try
tag=get_random_stuff.get_random_tag
instead of
tag=get_random_stuff.get_random_tag()
this will call your generator everytime a recipe is created, instead of only calling during recipe module evaluation.
@vandersonmota it worked! thanks a lot. I close the issue but because I am still a newbie and couldn't find the right answer on the Internet... could you please explain me why is it important to remove the parenthesis? (i guess this is more Python related than Model Mommy related)
Read: http://model-mommy.readthedocs.org/en/latest/recipes.html#recipes-with-callables
when you did:
tag=get_random_stuff.get_random_tag()
the tag was assigned with the value generated by "get_random_tag", because you already called the function, when you were defining your recipe. When you pass a callable, mommy detects it and calls your function during mommy.make_recipe.
All clear now, thanks a lot!
I am trying to make a simple test which should create 20 instances of a model called TagModel using Model Mommy recipes.
It seems that the generator I use to fill the tag field is only called once even if I am trying to generate 20 instances, so the test is giving me a "duplicate key value violates unique constraint "core_tagmodel_tag_key".
My recipe in mommy_recipes is:
In test_models.py I create the following simple test:
And this is the generator method I use (get_random_tag()):