From 04fe33d64d2f4191a958de42729c2e69ce2faf5a Mon Sep 17 00:00:00 2001 From: Bartosz Dabrowski Date: Mon, 19 Aug 2024 15:18:15 +0200 Subject: [PATCH 1/4] Fixed bug in checking the existence of custom __init__ method in an object. The __init__ wasn't called previously. --- polymorphic/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index 8e93281a..7d8636d1 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -73,7 +73,7 @@ def transmogrify(cls, obj): """ Upcast a class to a different type without asking questions. """ - if "__init__" not in obj.__dict__: + if "__init__" not in obj.__class__.__dict__: # Just assign __class__ to a different value. new = obj new.__class__ = cls From 978f9c701adc429947d99503c0057439b29b2aaf Mon Sep 17 00:00:00 2001 From: Bartosz Dabrowski Date: Tue, 20 Aug 2024 14:11:11 +0200 Subject: [PATCH 2/4] Added unit-tests for #615 --- ...eadduck_delete_inlinemodelbase_and_more.py | 36 +++++++++++++++++++ polymorphic/tests/models.py | 25 +++++++++++++ polymorphic/tests/test_query.py | 12 +++++++ 3 files changed, 73 insertions(+) create mode 100644 polymorphic/tests/migrations/0002_blueheadduck_delete_inlinemodelbase_and_more.py create mode 100644 polymorphic/tests/test_query.py diff --git a/polymorphic/tests/migrations/0002_blueheadduck_delete_inlinemodelbase_and_more.py b/polymorphic/tests/migrations/0002_blueheadduck_delete_inlinemodelbase_and_more.py new file mode 100644 index 00000000..64f86279 --- /dev/null +++ b/polymorphic/tests/migrations/0002_blueheadduck_delete_inlinemodelbase_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.15 on 2024-08-20 06:34 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='BlueHeadDuck', + fields=[ + ('duck_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tests.duck')), + ], + options={ + 'abstract': False, + 'base_manager_name': 'objects', + }, + bases=('tests.duck',), + ), + migrations.CreateModel( + name='PurpleHeadDuck', + fields=[ + ], + options={ + 'proxy': True, + 'indexes': [], + 'constraints': [], + }, + bases=('tests.blueheadduck',), + ), + ] diff --git a/polymorphic/tests/models.py b/polymorphic/tests/models.py index 76e1b626..d43bc33b 100644 --- a/polymorphic/tests/models.py +++ b/polymorphic/tests/models.py @@ -496,3 +496,28 @@ class SubclassSelectorProxyConcreteModel(SubclassSelectorProxyModel): class NonPolymorphicParent(PolymorphicModel, Group): test = models.CharField(max_length=22, default="test_non_polymorphic_parent") + + +# models for https://github.com/jazzband/django-polymorphic/issues/615 + + +class BlueHeadDuck(Duck): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # This wasn't run before the fix: + self.color = "blue" + + +class HomeDuck(models.Model): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.home = "Duckburg" + + class Meta: + abstract = True + + +# Not sure if this will be necessary... +class PurpleHeadDuck(HomeDuck, BlueHeadDuck): + class Meta: + proxy = True diff --git a/polymorphic/tests/test_query.py b/polymorphic/tests/test_query.py new file mode 100644 index 00000000..8d47b0d8 --- /dev/null +++ b/polymorphic/tests/test_query.py @@ -0,0 +1,12 @@ +from polymorphic.tests.models import Duck, PurpleHeadDuck + + +def test_transmogrify_with_init(db): + pur = PurpleHeadDuck.objects.create() + assert pur.color == "blue" + assert pur.home == "Duckburg" + + pur = Duck.objects.get(id=pur.id) + assert pur.color == "blue" + # issues/615 fixes following line: + assert pur.home == "Duckburg" From 28709ac63cbfe622da51014c054d8b94953f325e Mon Sep 17 00:00:00 2001 From: Bartosz Dabrowski Date: Tue, 20 Aug 2024 14:13:13 +0200 Subject: [PATCH 3/4] Removed comment --- polymorphic/tests/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/polymorphic/tests/models.py b/polymorphic/tests/models.py index d43bc33b..606cefd5 100644 --- a/polymorphic/tests/models.py +++ b/polymorphic/tests/models.py @@ -517,7 +517,6 @@ class Meta: abstract = True -# Not sure if this will be necessary... class PurpleHeadDuck(HomeDuck, BlueHeadDuck): class Meta: proxy = True From fcc8751b4e3b7b8fb28ec9245184fa90272c7278 Mon Sep 17 00:00:00 2001 From: Bartosz Dabrowski Date: Tue, 20 Aug 2024 14:13:50 +0200 Subject: [PATCH 4/4] Removed comment --- polymorphic/tests/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/polymorphic/tests/models.py b/polymorphic/tests/models.py index 606cefd5..8116f009 100644 --- a/polymorphic/tests/models.py +++ b/polymorphic/tests/models.py @@ -504,7 +504,6 @@ class NonPolymorphicParent(PolymorphicModel, Group): class BlueHeadDuck(Duck): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - # This wasn't run before the fix: self.color = "blue"