Skip to content
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
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
v4.2.0 (2025-12-04)
-------------------

* Fixed `The objects which were transmogrified aren't initialized correctly if they implement __init__ method. <https://github.com/jazzband/django-polymorphic/issues/615>`_
* Implemented `Defer to chunk_size parameter on .iterators for fetching get_real_instances() <https://github.com/jazzband/django-polymorphic/pull/672>`_
* Fixed `Show full admin context (breadcrumb and logout nav) in model type selection admin form <https://github.com/jazzband/django-polymorphic/pull/580>`_
* Fixed `Issue with Autocomplete Fields in StackedPolymorphicInline.Child Inline <https://github.com/jazzband/django-polymorphic/issues/546>`_
Expand Down
2 changes: 1 addition & 1 deletion src/polymorphic/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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
Expand Down
50 changes: 36 additions & 14 deletions src/polymorphic/tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.26 on 2025-12-02 16:47
# Generated by Django 4.2.26 on 2025-12-03 23:29

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -12,8 +12,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('contenttypes', '0002_remove_content_type_name'),
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
Expand Down Expand Up @@ -56,6 +56,18 @@ class Migration(migrations.Migration):
},
bases=(polymorphic.showfields.ShowFieldTypeAndContent, models.Model),
),
migrations.CreateModel(
name='Duck',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30)),
('polymorphic_ctype', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_%(app_label)s.%(class)s_set+', to='contenttypes.contenttype')),
],
options={
'abstract': False,
'base_manager_name': 'objects',
},
),
migrations.CreateModel(
name='Enhance_Base',
fields=[
Expand Down Expand Up @@ -315,6 +327,17 @@ class Migration(migrations.Migration):
},
bases=('tests.blogbase',),
),
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='CustomPkInherit',
fields=[
Expand Down Expand Up @@ -833,18 +856,6 @@ class Migration(migrations.Migration):
'base_manager_name': 'objects',
},
),
migrations.CreateModel(
name='Duck',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30)),
('polymorphic_ctype', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_%(app_label)s.%(class)s_set+', to='contenttypes.contenttype')),
],
options={
'abstract': False,
'base_manager_name': 'objects',
},
),
migrations.CreateModel(
name='DateModel',
fields=[
Expand Down Expand Up @@ -1108,6 +1119,17 @@ class Migration(migrations.Migration):
},
bases=(polymorphic.showfields.ShowFieldTypeAndContent, models.Model),
),
migrations.CreateModel(
name='PurpleHeadDuck',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('tests.blueheadduck', models.Model),
),
migrations.CreateModel(
name='Model2D',
fields=[
Expand Down
20 changes: 20 additions & 0 deletions src/polymorphic/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,23 @@ def __str__(self):
class Team(models.Model):
team_name = models.CharField(max_length=100)
user_profiles = models.ManyToManyField(UserProfile, related_name="user_teams")


class BlueHeadDuck(Duck):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color = "blue"


class HomeDuck(models.Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.home = "Duckburg"

class Meta:
abstract = True


class PurpleHeadDuck(HomeDuck, BlueHeadDuck):
class Meta:
proxy = True
13 changes: 12 additions & 1 deletion src/polymorphic/tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
ChildModelWithManager,
CustomPkBase,
CustomPkInherit,
Duck,
Enhance_Base,
Enhance_Inherit,
InitTestModelSubclass,
Expand Down Expand Up @@ -90,6 +89,8 @@
UUIDPlainC,
UUIDProject,
UUIDResearchProject,
Duck,
PurpleHeadDuck,
)


Expand Down Expand Up @@ -1411,3 +1412,13 @@ def test_iteration(self):
# known deletion issue with oracle
# https://github.com/jazzband/django-polymorphic/issues/673
pass

def test_transmogrify_with_init(self):
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"