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

fix and test regression in 1.18.3 forbidding setting GFK by a function #515

Merged
merged 3 commits into from
Feb 11, 2025
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Removed

## [1.20.2](https://pypi.org/project/model-bakery/1.20.2)

### Changed

- Fix setting GFK parameter by a callable (#516)


## [1.20.1](https://pypi.org/project/model-bakery/1.20.1/)

### Added
Expand Down
2 changes: 2 additions & 0 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
ct_field_name = data["content_type_field"]
oid_field_name = data["object_id_field"]
value = data["value"]
if callable(value):
value = value()
if is_iterator(value):
value = next(value)
if value is None:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,20 @@ def test_create_model_with_contenttype_field(self):
assert isinstance(dummy, models.DummyGenericForeignKeyModel)
assert isinstance(dummy.content_type, ContentType)

def test_create_model_with_contenttype_with_content_object(self):
"""Test creating model with contenttype field and populating that field by function."""
from django.contrib.contenttypes.models import ContentType

def get_dummy_key():
return baker.make("Person")

dummy = baker.make(
models.DummyGenericForeignKeyModel, content_object=get_dummy_key
)
assert isinstance(dummy, models.DummyGenericForeignKeyModel)
assert isinstance(dummy.content_type, ContentType)
assert isinstance(dummy.content_object, models.Person)


@pytest.mark.skipif(
not BAKER_CONTENTTYPES, reason="Django contenttypes framework is not installed"
Expand Down
Loading