Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using make_recipe with _quantity together with related key only applies to last item created #28

Closed
urbnjamesmi1 opened this issue Apr 18, 2019 · 5 comments · Fixed by #480

Comments

@urbnjamesmi1
Copy link

Using make_recipe with _quantity together with related key only applies to last item created

Given this setup code:

mommy_recipes.py:

dog1 = recipe.Recipe(
     Dog
)

dog2 = recipe.Recipe(
    Dog
)

company = recipe.Recipe(
    Company
)

incomplete_person = recipe.Recipe(
    Person,
    dog_set=recipe.related(dog1, dog2)
)

complete_person = incomplete_person.extend(
    company=recipe.foreign_key(company)
)

In a test python file:

people = mommy.make_recipe("complete_person", _quantity=3)

Expected behavior

That each model in people has the 2 dog instances.

Actual behavior

Only the last item in people has the 2 dog instances. The first 2 are empty.

Reproduction Steps

How to reproduce this issue.

See summary above

Versions

Python: 3.6
Django: 1.9
Model Mommy: 1.6.0

@urbnjamesmi1
Copy link
Author

I was able to work around this by calling mommy.make_recipe("complete_person") in a list comprehension, but it's a bug if I use the _quantity keyword

@danizen
Copy link

danizen commented Sep 20, 2019

Declaring a kwargs that holds the attrs that transcend one recipe seems a more flexible workaround.

# dog1, dog2, and company as above

person_kwargs={
    'dog_set': recipe.related(dog1, dog2)
)

unemployed_person = Recipe(Person,
    **person_kwargs
)

employed_person = Recipe(Person, 
    company=recipe.foreign_key(company)
    **person_kwargs
)

NOTE: I also reject the notion that you need a company to be complete ;).

@urbnjamesmi1
Copy link
Author

Then extend doesn't truly extend in all cases. You have to explicitly remember these type of arguments and explicitly specify them each time? That should at least be documented, but it would be nice to have it fixed.

@danizen
Copy link

danizen commented Sep 20, 2019

@urbnjamesmi1, yes it is a bug that should be fixed, but wanted to share a possibly useful workaround.

@berinhard berinhard transferred this issue from berinhard/model_mommy Oct 22, 2019
lucasrcezimbra added a commit to lucasrcezimbra/model_bakery that referenced this issue Jun 20, 2024
@amureki
Copy link
Collaborator

amureki commented Jun 28, 2024

Released in 1.18.2, thanks to @lucasrcezimbra ! 🎉

amureki added a commit that referenced this issue Aug 9, 2024
* origin/main:
  Bump 1.19.0
  Fix #483 -- Add Django 5.1 support (#485)
  Bump 1.18.3
  Refs #416 -- Allow combination of GFK and `_fill_optional` (#438)
  Bump 1.18.2
  Update ruff CI syntax (#481)
  Fix #28 -- allow make_recipe to work with _quantity (#480)
  Bump 1.18.1
  Replace expensive `count()` with cheap `exists()` (#478)
  Update CI Python version to 3.12
  Delete hard action requirement for a changelog
  Bump 1.18.0
  Fix #265 -- drop hard dependency on `contenttypes` framework (#476)
  Bump GitHub Actions artifacts to v4 (#470)
  Drop Django 3.2 support (#475)
  Bump actions/setup-python from 4 to 5 (#466)
  Drop Django 4.1 support (reached end of life) (#465)
  Support Django 5.0 (#464)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants