#9318: "Virtual" behaviour for signal dispatcher and model inheritance
-------------------------------------+-------------------------------------
     Reporter:  Alexander Artemenko  |                    Owner:  (none)
         Type:  New feature          |                   Status:  new
    Component:  Core (Other)         |                  Version:  1.0
     Severity:  Normal               |               Resolution:
     Keywords:  model inheritance,   |             Triage Stage:  Accepted
  signals, dispatch, proxy,          |
  subclass                           |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by rgangopadhya):

 To handle pre_save and all other signals, I took the approach of sending
 all signals to the parent model when triggered by child models:


 {{{
 from django.db import models
 from django.db.models.signals import (
     pre_save,
     post_save,
     pre_delete,
     post_delete,
     m2m_changed,
 )

 class A(models.Model): pass

 class B(A):
     class Meta:
         proxy = True

 signals = [
     pre_save,
     post_save,
     pre_delete,
     post_delete,
     m2m_changed,
 ]

 def make_sender_fn(model, signal):

     def sender_fn(sender, **kwargs):
         # signal send method passes itself
         # as a signal kwarg to its receivers
         kwargs.pop('signal')
         signal.send(sender=model, **kwargs)

     return sender_fn

 def connect_all_signals(from_model, to_model):
     # connecting signals from one model to another
     # is useful when from_model inherits from to_model
     # and hence should take on all behavior from the
     # parent model
     # Django does not send signals for parent model
     # when child is triggered
     # https://code.djangoproject.com/ticket/9318#no1
     for signal in signals:
         signal.connect(
             make_sender_fn(to_model, signal),
             sender=from_model,
             weak=False,
         )

 connect_all_signals(B, A)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/9318#comment:37>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.89435f2acdcdbf212101deca2d596e49%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to