#27350: Attaching signals to abstract models don't work as it used to be
----------------------------------------------+--------------------
     Reporter:  thoas                         |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.10
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Hi,

 I'm currently porting an application from Django 1.9.x to 1.10.x, we are
 relying on managers to attach signals to allow developers overriding them
 in our generic applications, this does not work anymore.

 A complete example:


 {{{
 from django.db import models
 from django.db.models import signals


 class Poll(models.Model):
     question = models.CharField(max_length=255)
     answers_count = models.PositiveIntegerField(default=0)


 class AnswerManager(models.Manager):
     def contribute_to_class(self, cls, name):
         signals.post_save.connect(self.post_save, sender=cls)

         return super(AnswerManager, self).contribute_to_class(cls, name)

     def post_save(self, instance, **kwargs):
         poll = instance.poll
         poll.answers_count = poll.answers.count()
         poll.save(update_fields=('answers_count', ))


 class AbstractAnswer(models.Model):
     text = models.CharField(max_length=255)
     poll = models.ForeignKey(Poll, related_name='answers')

     objects = AnswerManager()

     class Meta:
         abstract = True


 class Answer(AbstractAnswer):
     class Meta:
         abstract = False


 class ModelsTests(TestCase):
     def test_simple_compute(self):
         poll = Poll.objects.create(question='Weird?')

         Answer.objects.create(poll=poll, text='Yes')

         # refresh
         poll = Poll.objects.get(pk=poll.pk)

         # still 0 on Django 1.10 and 1 on Django 1.9
         assert poll.answers_count == 1
 }}}


 The complete application is available [[https://github.com/thoas/django-
 signals-wtf|here]] and travis is available [[https://travis-ci.org/thoas
 /django-signals-wtf/builds/167669153|here]] to test again both 1.10 and
 1.9.

 Is this some kind of undocumented new behavior or a bug?

--
Ticket URL: <https://code.djangoproject.com/ticket/27350>
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/048.8df3e5cb195ff8c952055b6a15facdad%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to