model-bakers / model_bakery

Object factory for Django
https://model-bakery.readthedocs.io/en/latest/
Other
846 stars 85 forks source link

Using `prepare` with generic foreign keys #492

Closed jeremy-engel closed 3 months ago

jeremy-engel commented 3 months ago

The changes in #438 prevent generic foreign keys passed to baker.prepare from being filled correctly. The _handle_generic_foreign_keys method is never called since _commit is False. The following test case added to the class TestFillingGenericForeignKeyField demonstrates the issue:

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

        profile = baker.make(models.Profile)
        dummy = baker.prepare(
            models.DummyGenericForeignKeyModel, content_object=profile
        )

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

        assert dummy.content_object == profile
        assert dummy.content_type == expected_content_type
        assert dummy.object_id == profile.pk
amureki commented 3 months ago

Hey Jeremy!

Thank you for the report, and extra thanks for providing a test case!

I released 1.19.2 that fixes that bug: