26

Is there a way to see which signals have been set in Django?

4 Answers 4

34

It's not really exposed in docs but Signal is just a class that contains a list of receivers which are called on event. You can manually check this list:

from django.db.models.signals import *

for signal in [pre_save, pre_init, pre_delete, post_save, post_delete, post_init, post_syncdb]:
    # print a List of connected listeners
    print signal.receivers
2
  • 5
    You listed 7 signals, and these are correct. But to get the list algorithmically, from django.db.models import signals and then [s for s in vars(signals).values() if isinstance(s, signals.ModelSignal)]
    – AlanSE
    May 17, 2018 at 14:17
  • 1
    Also, by using that list, I find that post_syncdb appears to be renamed to post_migrate.
    – AlanSE
    May 17, 2018 at 14:25
12

There's a django app called django-debug-toolbar which adds a little toolbar at the top of all django served pages providing info related to the backend of the page's rendering, such as how many queries were executed, how much time they each took, etc. It also prints out signals. I don't use signals in my app, so I have never used that feature, but it's there.

8

If you want to list only the connected receivers for a specific signal on a specific model, you can look at _live_receivers. For instance, if you want to list the connected post_save hooks for a model named MyModel, you can do:

from django.db.models.signals import post_save
from models import MyModel
print(post_save._live_receivers(MyModel))

I found this approach in the Django source code by looking for how has_listeners works: https://github.com/django/django/blob/3eb679a86956d9eedf24492f0002de002f7180f5/django/dispatch/dispatcher.py#L153

1
  • 1
    It's generally a bad idea to use non-public methods. They are marked with '_' prefix for a reason.
    – Alex K
    Jul 15, 2022 at 12:18
7

I wrote little command that shows all signal listeners: https://gist.github.com/1264102

You can modify it to show signals only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.