Skip to content

Commit cf367f0

Browse files
authored
Merge pull request #846 from bckohan/master
fix base_manager resolution
2 parents 0e5e659 + 5c9d399 commit cf367f0

File tree

14 files changed

+927
-859
lines changed

14 files changed

+927
-859
lines changed

docs/changelog/index.rst

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

4+
v4.9.1, v4.10.3 (2026-01-15)
5+
----------------------------
6+
7+
* Fixed `Meta.base_manager_name is not respected <https://github.com/jazzband/django-polymorphic/issues/845>`_
8+
9+
410
v4.10.2 (2026-01-14)
511
--------------------
612

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.10.2"
7+
version = "4.10.3"
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.10.2"
22+
VERSION = "4.10.3"
2323

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

src/polymorphic/base.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,28 @@ class PolymorphicModelBase(ModelBase):
7171
"""
7272

7373
def __new__(cls, model_name, bases, attrs, **kwargs):
74-
# create new model
75-
new_class = super().__new__(cls, model_name, bases, attrs, **kwargs)
74+
# skip special setup for PolymorphicModel itself
75+
if attrs.pop("_meta_skip", False):
76+
return super().__new__(cls, model_name, bases, attrs, **kwargs)
7677

77-
if new_class._meta.base_manager_name is None:
78-
# by default, use polymorphic manager as the base manager
79-
new_class._meta.base_manager_name = new_class._meta.default_manager_name or "objects"
78+
from .models import PolymorphicModel
8079

81-
# ensure base_manager is a plain PolymorphicManager by resetting it if it
82-
# was not explicitly set and it defaults to a changed default_manager
83-
# the base class manager determination logic is complex enough that we prefer
84-
# to observe its application and correct rather than preempting it
85-
if (
86-
type(new_class._meta.default_manager) is not PolymorphicManager
87-
and new_class._meta.base_manager is new_class._meta.default_manager
80+
new_class = super().__new__(cls, model_name, bases, attrs, **kwargs)
81+
82+
if not any(
83+
parent._meta.base_manager_name
84+
for parent in new_class.mro()
85+
if issubclass(parent, PolymorphicModel)
8886
):
89-
manager = PolymorphicManager()
90-
manager.name = "_base_manager"
91-
manager.model = new_class
92-
manager.auto_created = True
93-
new_class._meta.base_manager_name = None
94-
# write new manager to property cache
95-
new_class._meta.__dict__["base_manager"] = manager
87+
if new_class._meta.default_manager.__class__ is PolymorphicManager:
88+
new_class._meta.__dict__["base_manager"] = new_class._meta.default_manager
89+
else:
90+
manager = PolymorphicManager()
91+
manager.name = "_base_manager"
92+
manager.model = new_class
93+
manager.auto_created = True
94+
# write new manager to property cache
95+
new_class._meta.__dict__["base_manager"] = manager
9696

9797
# wrap on_delete handlers of reverse relations back to this model with the
9898
# polymorphic deletion guard
@@ -104,8 +104,6 @@ def __new__(cls, model_name, bases, attrs, **kwargs):
104104

105105
# replace the parent/child descriptors
106106
if new_class._meta.parents and not (new_class._meta.abstract or new_class._meta.proxy):
107-
# PolymorphicModel is guaranteed to be defined here
108-
from .models import PolymorphicModel
109107

110108
def replace_inheritance_descriptors(model):
111109
for super_cls, field_to_super in model._meta.parents.items():

src/polymorphic/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class PolymorphicModel(models.Model, metaclass=PolymorphicModelBase):
3535
and provides a polymorphic manager as the default manager (and as 'objects').
3636
"""
3737

38+
_meta_skip = True
39+
3840
# for PolymorphicModelBase, so it can tell which models are polymorphic and which are not (duck typing)
3941
polymorphic_model_marker = True
4042

0 commit comments

Comments
 (0)