#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 Soheil Damangir):

 A quick fix without a need to patch could be to override the save method
 for the parent mode:

 {{{
  from django.db import models

  class MyModel(models.Model):
     """
     >>> m = MyModel(name="The model")
     >>> m.save()
     Hello!
     """
     name = models.CharField(max_length=10)
     def save(self, *args, **kwargs):
         # To trigger the signals for parent model
         klass = self.__class__
         try:
             if self.__class__._meta.proxy:
                 self.__class__ = self.__class__._meta.concrete_model
             super(MyModel, self).save(*args, **kwargs)
         finally:
             self.__class__ = klass

  class ProxyModel(MyModel):
     """
     >>> p = ProxyModel(name="Proxy")
     >>> p.save()
     Hello!
     """
     class Meta:
         proxy = True


  from django.db.models.signals import post_save

  def say_hello(sender, *args, **kwargs):
      print "Hello!"
  post_save.connect(say_hello, sender=Parent)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/9318#comment:36>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.86e657a6a6ec1c219ef2cf20e8fbfbf4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to