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.19.3 forbidding setting GFK via proxy model #517

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

### Removed

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

### Changed

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

- Fix setting GFK parameter by a callable
- Fix regression forbidding using Proxy models as GFK

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

Expand Down
4 changes: 3 additions & 1 deletion model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,9 @@ def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
setattr(
instance,
ct_field_name,
contenttypes_models.ContentType.objects.get_for_model(value),
contenttypes_models.ContentType.objects.get_for_model(
value, for_concrete_model=False
),
)
setattr(instance, oid_field_name, value.pk)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,24 @@ def get_dummy_key():
assert isinstance(dummy.content_type, ContentType)
assert isinstance(dummy.content_object, models.Person)

def test_create_model_with_contenttype_field_and_proxy_model(self):
from django.contrib.contenttypes.models import ContentType

class ProxyPerson(models.Person):
class Meta:
proxy = True
app_label = "generic"

dummy = baker.make(
models.DummyGenericForeignKeyModel,
content_object=baker.make(ProxyPerson, name="John Doe"),
)
dummy.refresh_from_db()
assert isinstance(dummy, models.DummyGenericForeignKeyModel)
assert isinstance(dummy.content_type, ContentType)
assert isinstance(dummy.content_object, ProxyPerson)
assert dummy.content_object.name == "John Doe"


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