Skip to content

Commit 52355a8

Browse files
PetrDlouhyamureki
andauthored
Fix #516 -- fix regression forbidding setting GFK by a function (#515)
* fix setting GFK by a callable, add test * update CHANGELOG.md * Fix linting --------- Co-authored-by: Rust Saiargaliev <[email protected]>
1 parent 1aa6662 commit 52355a8

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313

1414
### Removed
1515

16+
## [1.20.2](https://pypi.org/project/model-bakery/1.20.2)
17+
18+
### Changed
19+
20+
- Fix setting GFK parameter by a callable (#516)
21+
22+
1623
## [1.20.1](https://pypi.org/project/model-bakery/1.20.1/)
1724

1825
### Added

model_bakery/baker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
699699
ct_field_name = data["content_type_field"]
700700
oid_field_name = data["object_id_field"]
701701
value = data["value"]
702+
if callable(value):
703+
value = value()
702704
if is_iterator(value):
703705
value = next(value)
704706
if value is None:

tests/test_baker.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,20 @@ def test_create_model_with_contenttype_field(self):
635635
assert isinstance(dummy, models.DummyGenericForeignKeyModel)
636636
assert isinstance(dummy.content_type, ContentType)
637637

638+
def test_create_model_with_contenttype_with_content_object(self):
639+
"""Test creating model with contenttype field and populating that field by function."""
640+
from django.contrib.contenttypes.models import ContentType
641+
642+
def get_dummy_key():
643+
return baker.make("Person")
644+
645+
dummy = baker.make(
646+
models.DummyGenericForeignKeyModel, content_object=get_dummy_key
647+
)
648+
assert isinstance(dummy, models.DummyGenericForeignKeyModel)
649+
assert isinstance(dummy.content_type, ContentType)
650+
assert isinstance(dummy.content_object, models.Person)
651+
638652

639653
@pytest.mark.skipif(
640654
not BAKER_CONTENTTYPES, reason="Django contenttypes framework is not installed"

0 commit comments

Comments
 (0)