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

baker.make() with _bulk_create=True fails for M2M-fields that have a reverse_name #489

Open
richardebeling opened this issue Aug 12, 2024 · 0 comments

Comments

@richardebeling
Copy link

richardebeling commented Aug 12, 2024

Follow-Up to #385, which was fixed in #486. When specifying values for a M2M field that has a reverse_name, baker currently tries to create an instance of the through-model using the related name when _bulk_create=True is passed.

Add this test case to TestCreateM2MWhenBulkCreate. Note that it is very similar to test_create, just the model used is switched from Classroom to Store, since Store.customers specifies a related_name.

def test_create_with_field_with_related_name(self):
    person = baker.make(models.Person)

    with self.assertNumQueries(11):
        baker.make(
            models.Store, customers=[person], _quantity=10, _bulk_create=True
        )
    s1, s2 = models.Store.objects.all()[:2]
    assert list(s1.customers.all()) == list(s2.customers.all()) == [person]

The test fails with this error:

___TestCreateM2MWhenBulkCreate.test_create_with_field_with_related_name ___
tests/test_baker.py:1090: in test_create_with_field_with_related_name
    baker.make(
model_bakery/baker.py:131: in make
    return bulk_create(baker, _quantity, _save_kwargs=_save_kwargs, **attrs)
model_bakery/baker.py:857: in bulk_create
    [
model_bakery/baker.py:858: in <listcomp>
    through_model(
../../.local/lib/python3.10/site-packages/django/db/models/base.py:567: in __init__
    raise TypeError(
E   TypeError: Store_customers() got unexpected keyword arguments: 'favorite_stores'

Versions

  • Python: 3.10.12
  • Django: 4.2
  • Model Bakery 1.19.1 (tested at d758c7a)
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

No branches or pull requests

1 participant