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

Support iterators on GFK fields when using _quantity param #207

Merged
merged 4 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added
- Add support for iterators on GFK fields when using _quantity param [PR #207](https://github.com/model-bakers/model_bakery/pull/207)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def prepare(self, _save_related=False, **attrs: Any) -> Model:
return self._make(commit=False, commit_related=_save_related, **attrs)

def get_fields(self) -> Any:
return self.model._meta.fields + self.model._meta.many_to_many
return set(self.model._meta.get_fields()) - set(self.get_related())

def get_related(
self,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ def test_filling_content_type_field(self):
assert isinstance(dummy.content_type, ContentType)
assert dummy.content_type.model_class() is not None

def test_iteratively_filling_generic_foreign_key_field(self):
"""
Ensures private_fields are included in Baker.get_fields(), otherwise
calling next() when a GFK is in iterator_attrs would be bypassed.
"""
objects = baker.make(models.Profile, _quantity=2)
dummies = baker.make(
models.DummyGenericForeignKeyModel,
content_object=iter(objects),
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens when no value is provided for this field? Consider adding a test for it.

Also, what happens to the content_type and object_id fields? Maybe there should be a test for those as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test to also verify the content_type and object_id field contain the expected values.

I'm unclear on whether the question "What happens when no value is provided for this field?" means what happens for the case baker.make(models.DummyGenericForeignKeyModel) or the case baker.make(models.DummyGenericForeignKeyModel, content_object=iter([])), since this PR specifically fixes iter being broken on a GFK. Did you want a test for the latter? The former case is already present in an existing test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I think you are right: the case I was thinking of is already covered. No action needed.

_quantity=2,
)

expected_content_type = ContentType.objects.get_for_model(models.Profile)

assert dummies[0].content_object == objects[0]
assert dummies[0].content_type == expected_content_type
assert dummies[0].object_id == objects[0].pk

assert dummies[1].content_object == objects[1]
assert dummies[1].content_type == expected_content_type
assert dummies[1].object_id == objects[1].pk


@pytest.mark.django_db
class TestFillingForeignKeyFieldWithDefaultFunctionReturningId:
Expand Down