Skip to content

Commit 2c9c9ef

Browse files
committed
add performance tests and fix performance regression #781
1 parent cb823d6 commit 2c9c9ef

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
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 (2026-01-01)
5+
-------------------
6+
7+
* Fixed `Significant performance regression on polymorphic queryset iteration <https://github.com/jazzband/django-polymorphic/issues/781>`_
8+
49
v4.3.0 (2025-12-09)
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.3.0"
7+
version = "4.3.1"
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.3.0"
22+
VERSION = "4.3.1"
2323

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

src/polymorphic/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ def __new__(self, model_name, bases, attrs, **kwargs):
7373
# for __init__ function of this class (monkeypatching inheritance accessors)
7474
new_class.polymorphic_super_sub_accessors_replaced = False
7575

76-
# determine the name of the primary key field and store it into the class variable
77-
# polymorphic_primary_key_name (it is needed by query.py)
7876
if new_class._meta.pk:
79-
new_class.polymorphic_primary_key_name = new_class._meta.pk.name
77+
new_class.polymorphic_primary_key_name = new_class._meta.pk.attname
8078

8179
return new_class
8280

src/polymorphic/tests/test_orm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ def test_subqueries(self):
15291529
def test_one_to_one_primary_key(self):
15301530
# check pk name resolution
15311531
for mdl in [Account, SpecialAccount1, SpecialAccount1_1, SpecialAccount2]:
1532-
assert mdl.polymorphic_primary_key_name == mdl._meta.pk.name
1532+
assert mdl.polymorphic_primary_key_name == mdl._meta.pk.attname
15331533

15341534
user1 = get_user_model().objects.create(
15351535
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: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)