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

CORE-3229: Forward signals from proxy models to their concrete models #10

Merged
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 django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.utils.version import get_version

VERSION = (4, 2, 6, "+hs", 2)
VERSION = (4, 2, 6, "+hs", 3)

__version__ = get_version(VERSION)

Expand Down
17 changes: 10 additions & 7 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,17 @@ class ModelState:
class Model(AltersData, metaclass=ModelBase):
def __init__(self, *args, **kwargs):
# Alias some things as locals to avoid repeat global lookups
cls = self.__class__
cls = sender = self.__class__
opts = self._meta
_setattr = setattr
_DEFERRED = DEFERRED
if opts.abstract:
raise TypeError("Abstract models cannot be instantiated.")

pre_init.send(sender=cls, args=args, kwargs=kwargs)
# CORE-3229: Forward signals from proxy models to their concrete models
if sender._meta.proxy:
sender = sender._meta.concrete_model
pre_init.send(sender=sender, args=args, kwargs=kwargs)

# Set up the storage for instance state
self._state = ModelState()
Expand Down Expand Up @@ -572,7 +575,7 @@ def __init__(self, *args, **kwargs):
f"{unexpected_names}"
)
super().__init__()
post_init.send(sender=cls, instance=self)
post_init.send(sender=sender, instance=self)

@classmethod
def from_db(cls, db, field_names, values):
Expand Down Expand Up @@ -855,14 +858,14 @@ def save_base(
using = using or router.db_for_write(self.__class__, instance=self)
assert not (force_insert and (force_update or update_fields))
assert update_fields is None or update_fields
cls = origin = self.__class__
# Skip proxies, but keep the origin as the proxy model.
cls = self.__class__
# CORE-3229: Forward signals from proxy models to their concrete models
if cls._meta.proxy:
cls = cls._meta.concrete_model
meta = cls._meta
if not meta.auto_created:
pre_save.send(
sender=origin,
sender=cls,
instance=self,
raw=raw,
using=using,
Expand Down Expand Up @@ -893,7 +896,7 @@ def save_base(
# Signal that the save is complete
if not meta.auto_created:
post_save.send(
sender=origin,
sender=cls,
instance=self,
created=(not updated),
update_fields=update_fields,
Expand Down
8 changes: 8 additions & 0 deletions django/db/models/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ def delete(self):
# send pre_delete signals
for model, obj in self.instances_with_model():
if not model._meta.auto_created:
# CORE-3229: Forward signals from proxy models to their concrete
# models
if model._meta.proxy:
model = model._meta.concrete_model
signals.pre_delete.send(
sender=model,
instance=obj,
Expand Down Expand Up @@ -508,6 +512,10 @@ def delete(self):
deleted_counter[model._meta.label] += count

if not model._meta.auto_created:
# CORE-3229: Forward signals from proxy models to their concrete
# models
if model._meta.proxy:
model = model._meta.concrete_model
for obj in instances:
signals.post_delete.send(
sender=model,
Expand Down
Loading