Skip to content

Commit 38e6c91

Browse files
committed
fix #781
1 parent 7344d60 commit 38e6c91

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
v4.3.1, v4.4.2 (2026-01-01)
5+
---------------------------
6+
7+
* Fixed `Significant performance regression on polymorphic queryset iteration <https://github.com/jazzband/django-polymorphic/pull/781>`_
8+
49
v4.4.1 (2025-12-15)
510
-------------------
611

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "django-polymorphic"
7-
version = "4.4.1"
7+
version = "4.4.2"
88
description = "Seamless polymorphic inheritance for Django models."
99
readme = "README.md"
1010
license = "BSD-3-Clause"

src/polymorphic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Seamless Polymorphic Inheritance for Django Models
2020
"""
2121

22-
VERSION = "4.4.1"
22+
VERSION = "4.4.2"
2323

2424
__title__ = "Django Polymorphic"
2525
__version__ = VERSION # version synonym for backwards compatibility

src/polymorphic/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __new__(cls, model_name, bases, attrs, **kwargs):
7373
# determine the name of the primary key field and store it into the class variable
7474
# polymorphic_primary_key_name (it is needed by query.py)
7575
if new_class._meta.pk:
76-
new_class.polymorphic_primary_key_name = new_class._meta.pk.name
76+
new_class.polymorphic_primary_key_name = new_class._meta.pk.attname
7777

7878
return new_class
7979

src/polymorphic/tests/test_orm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def test_subqueries(self):
15541554
def test_one_to_one_primary_key(self):
15551555
# check pk name resolution
15561556
for mdl in [Account, SpecialAccount1, SpecialAccount1_1, SpecialAccount2]:
1557-
assert mdl.polymorphic_primary_key_name == mdl._meta.pk.name
1557+
assert mdl.polymorphic_primary_key_name == mdl._meta.pk.attname
15581558

15591559
user1 = get_user_model().objects.create(
15601560
username="user1", email="[email protected]", password="password"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from django.test import TransactionTestCase
2+
from polymorphic.tests.models import (
3+
Model2A,
4+
Model2B,
5+
Model2C,
6+
Model2D,
7+
)
8+
9+
10+
class PerformanceTests(TransactionTestCase):
11+
def test_baseline_number_of_queries(self):
12+
"""
13+
Test that the number of queries for loading polymorphic models is within
14+
expected limits.
15+
"""
16+
for idx in range(100):
17+
Model2A.objects.create(field1=f"A{idx}")
18+
Model2B.objects.create(field1=f"A{idx}", field2=f"B{idx}")
19+
Model2C.objects.create(field1=f"A{idx}", field2=f"B{idx}", field3=f"C{idx}")
20+
Model2D.objects.create(
21+
field1=f"A{idx}", field2=f"B{idx}", field3=f"C{idx}", field4=f"D{idx}"
22+
)
23+
24+
with self.assertNumQueries(4):
25+
list(Model2A.objects.all().order_by("pk"))
26+
27+
with self.assertNumQueries(3):
28+
list(Model2B.objects.all().order_by("pk"))
29+
30+
with self.assertNumQueries(2):
31+
list(Model2C.objects.all().order_by("pk"))
32+
33+
with self.assertNumQueries(1):
34+
list(Model2D.objects.all().order_by("pk"))

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)