Skip to content

Commit 11d2b74

Browse files
authored
Merge pull request #679 from jazzband/test-fk-abstract
Make related polymorphic object manager selection more robust to multiple inheritance scenarios.
2 parents 18be22d + 145bf57 commit 11d2b74

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
v4.3.0 (202X-XX-XX)
55
-------------------
6+
7+
* Fixed `Foreign key resolves to parent class when using abstract models <https://github.com/jazzband/django-polymorphic/issues/437>`_
68
* Fixed `Support Q expressions that contain subquery expressions <https://github.com/jazzband/django-polymorphic/pull/572>`_
79

810
v4.2.0 (2025-12-04)

src/polymorphic/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def __new__(self, model_name, bases, attrs, **kwargs):
6060
# create new model
6161
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)
6262

63+
if new_class._meta.base_manager_name is None:
64+
# by default, use polymorphic manager as the base manager - i.e. for
65+
# related fields etc. This could happen in multi-inheritance scenarios
66+
# where one parent is polymorphic and the other not and the non poly parent
67+
# is higher in the MRO
68+
new_class._meta.base_manager_name = "objects"
69+
6370
# check if the model fields are all allowed
6471
self.validate_model_fields(new_class)
6572

src/polymorphic/tests/migrations/0001_initial.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.2.26 on 2025-12-04 12:57
1+
# Generated by Django 4.2.26 on 2025-12-05 02:34
22

33
from django.db import migrations, models
44
import django.db.models.deletion
@@ -256,7 +256,6 @@ class Migration(migrations.Migration):
256256
],
257257
options={
258258
'abstract': False,
259-
'base_manager_name': 'objects',
260259
},
261260
bases=(polymorphic.showfields.ShowFieldTypeAndContent, models.Model),
262261
),
@@ -586,7 +585,6 @@ class Migration(migrations.Migration):
586585
],
587586
options={
588587
'abstract': False,
589-
'base_manager_name': 'objects',
590588
},
591589
bases=('tests.relationbase',),
592590
),
@@ -598,7 +596,6 @@ class Migration(migrations.Migration):
598596
],
599597
options={
600598
'abstract': False,
601-
'base_manager_name': 'objects',
602599
},
603600
bases=('tests.relationbase',),
604601
),
@@ -1060,7 +1057,6 @@ class Migration(migrations.Migration):
10601057
],
10611058
options={
10621059
'abstract': False,
1063-
'base_manager_name': 'objects',
10641060
},
10651061
bases=('tests.relationb',),
10661062
),

src/polymorphic/tests/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ class Enhance_Inherit(Enhance_Base, Enhance_Plain):
109109
field_i = models.CharField(max_length=30)
110110

111111

112-
class RelationBase(ShowFieldTypeAndContent, PolymorphicModel):
112+
class RelationAbstractModel(models.Model):
113+
class Meta:
114+
abstract = True
115+
116+
117+
class RelationBase(RelationAbstractModel, ShowFieldTypeAndContent, PolymorphicModel):
113118
field_base = models.CharField(max_length=30)
114119
fk = models.ForeignKey(
115120
"self", on_delete=models.CASCADE, null=True, related_name="relationbase_set"

src/polymorphic/tests/test_orm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def test_relation_base(self):
696696
oa.m2m.add(oa)
697697
oa.m2m.add(ob)
698698

699-
objects = RelationBase.objects.all()
699+
objects = RelationBase.objects.order_by("pk").all()
700700
assert (
701701
repr(objects[0])
702702
== '<RelationBase: id 1, field_base (CharField) "base", fk (ForeignKey) None, m2m (ManyToManyField) 0>'

0 commit comments

Comments
 (0)