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

Do not bypass many-to-many related fields. #299

Merged
merged 2 commits into from Mar 31, 2022
Merged

Do not bypass many-to-many related fields. #299

merged 2 commits into from Mar 31, 2022

Conversation

berinhard
Copy link
Member

Fixes #248

The issue has all the important discussion on the reasons behind this PR. But the if condition was bypassing related m2m and, thus, considering them as regular fields of the model.

@berinhard berinhard requested review from timjklein36, amureki and a team March 31, 2022 20:30
Comment on lines +234 to +241
class ClassroomM2MRelated(models.Model):
"""
This model was created in order to reproduce the scenario described
at issue 248 that is: a model with a M2M field (Classroom) being also used
as a M2M field from another model (ClassroomM2MRelated)
"""

related_classrooms = models.ManyToManyField(Classroom)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just by adding this model I was able to break 2 tests with the same error message as described by @a-edakin in #248. Here's the traceback of the failing tests to prove it:

______________________________________________________________ TestFillNullsTestCase.test_create_nullable_many_to_many_if_flagged_and_fill_optional _______________________________________________________________
tests/test_baker.py:621: in test_create_nullable_many_to_many_if_flagged_and_fill_optional
    classroom = baker.make(models.Classroom, make_m2m=True, _fill_optional=True)
model_bakery/baker.py:133: in make
    return baker.make(
model_bakery/baker.py:389: in make
    return self._make(**params)
model_bakery/baker.py:457: in _make
    self.model_attrs[field.name] = self.generate_value(
model_bakery/baker.py:660: in generate_value
    if field.has_default() and field.name not in self.rel_fields:
E   AttributeError: 'ManyToManyRel' object has no attribute 'has_default'
________________________________________________________ TestFillNullsTestCase.test_nullable_many_to_many_is_not_created_if_not_flagged_and_fill_optional _________________________________________________________
tests/test_baker.py:627: in test_nullable_many_to_many_is_not_created_if_not_flagged_and_fill_optional
    classroom = baker.make(models.Classroom, make_m2m=False, _fill_optional=True)
model_bakery/baker.py:133: in make
    return baker.make(
model_bakery/baker.py:389: in make
    return self._make(**params)
model_bakery/baker.py:457: in _make
    self.model_attrs[field.name] = self.generate_value(
model_bakery/baker.py:660: in generate_value
    if field.has_default() and field.name not in self.rel_fields:
E   AttributeError: 'ManyToManyRel' object has no attribute 'has_default'

@berinhard
Copy link
Member Author

Thanks @timjklein36 for the lightning speed review ⚡ ⚡ ⚡

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 this pull request may close these issues.

AttributeError: 'ManyToManyRel' object has no attribute 'has_default'
2 participants