Skip to content

Commit

Permalink
Fix #516 -- fix regression forbidding setting GFK by a function (#515)
Browse files Browse the repository at this point in the history
* fix setting GFK by a callable, add test

* update CHANGELOG.md

* Fix linting

---------

Co-authored-by: Rust Saiargaliev <[email protected]>
  • Loading branch information
PetrDlouhy and amureki authored Feb 11, 2025
1 parent 1aa6662 commit 52355a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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

0 comments on commit 52355a8

Please sign in to comment.