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

Fixed bug in checking the existence of custom __init__ method in an o… #614

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion polymorphic/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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',),
),
]
23 changes: 23 additions & 0 deletions polymorphic/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,26 @@ 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)
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
12 changes: 12 additions & 0 deletions polymorphic/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -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"
Loading