post_save signal isn't called

artem picture artem · Nov 22, 2016 · Viewed 11.6k times · Source

I've already read all related questions.

I have two Django projects, and signals work fine in one, but do not work in second one (I've just copy-pasted code and changed names respectively).

I have an orders app with Order model. App is included in INSTALLED_APPS setting.

I have app config in apps.py:

from django.apps import AppConfig


class OrdersConfig(AppConfig):
    name = 'orders'

    def ready(self):
        super(OrdersConfig, self).ready()

        # noinspection PyUnresolvedReferences
        import signals

__init__.py:

default_app_config = 'orders.apps.OrdersConfig'

And, finally, signals.py:

@receiver(post_save, sender=Order)
def order_save(sender, instance, created, **kwargs):
    print 'Post save'
    if created:
        print 'Created'
        send_email_new_order.delay(settings.MODERATOR_EMAIL, instance.pk)

And signal does not getting called. Why?

Django 1.10.3.

Answer

e4c5 picture e4c5 · Nov 30, 2016

When would post_save be fired?

What the document says: At the end of the save method.

What it really means: At the end of the successful completion of the save method.

When would the signal not be fired?

  1. If the save method does not successfully save the object (such as when an IntegrityError occurs)
  2. When you call MyModel.objects.update()
  3. When you override the save method and forget to call the superclass method.
  4. When your signal receiver hasn't been successfully registered.

How to register the receiver

Simplest is to use the @receiver decorator as you have done. The alternative is to use

from django.db.models.signals import pre_save

pre_save.connect(order_save, sender='app_label.MyModel')

Where should this code be placed?

Nowadays, the manual states that

Strictly speaking, signal handling and registration code can live anywhere you like, although it’s recommended to avoid the application’s root module and its models module to minimize side-effects of importing code.

That is probably why in this instance you have created a file called signals.py and place your code inside that and gone to all that trouble with the AppConfig class and the ready method. But funnily enough, the Django 1.6 manual says:

You can put signal handling and registration code anywhere you like. However, you’ll need to make sure that the module it’s in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app’s models.py a good place to put registration of signal handlers.

So if you are having trouble getting your signal receiver registered you can actually try putting your code in models.py or views.py and leave out the bits from AppConfig (maybe even remove AppConfig completely)

If you want to carry out the registration in AppConfig, and you are having trouble with @reciever and/or imports, you can try

from django.db.models.signals import pre_save
from app_label.signals import my_reciever

def ready(self):
    pre_save.connect(my_reciever, sender='app_label.MyModel')

How to avoid repeats?

Does the signal get fired twice? Make sure that you register the receiver only once. If you register it in AppConfig, leave it out of models.py and vice verce