Is there a way to see which signals have been set in Django?
有没有办法看到Django中设置了哪些信号?
4 个解决方案
#1
29
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:
它并没有真正暴露在文档中,但Signal只是一个包含事件调用的接收器列表的类。您可以手动检查此列表:
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
10
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.
有一个名为django-debug-toolbar的django应用程序,它在所有django服务页面的顶部添加了一个小工具栏,提供与页面渲染后端相关的信息,例如执行了多少查询,每次执行了多少时间等等它还打印出信号。我不在我的应用程序中使用信号,所以我从未使用过该功能,但它就在那里。
#3
7
I wrote little command that shows all signal listeners: https://gist.github.com/1264102
我写了一个显示所有信号监听器的小命令:https://gist.github.com/1264102
You can modify it to show signals only.
您可以修改它以仅显示信号。
#4
0
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:
如果您只想列出特定型号上特定信号的连接接收器,可以查看_live_receivers。例如,如果要列出名为MyModel的模型的已连接post_save挂钩,则可以执行以下操作:
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
我通过查找has_listeners如何工作在Django源代码中找到了这种方法:https://github.com/django/django/blob/3eb679a86956d9eedf24492f0002de002f7180f5/django/dispatch/dispatcher.py#L153
#1
29
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:
它并没有真正暴露在文档中,但Signal只是一个包含事件调用的接收器列表的类。您可以手动检查此列表:
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
10
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.
有一个名为django-debug-toolbar的django应用程序,它在所有django服务页面的顶部添加了一个小工具栏,提供与页面渲染后端相关的信息,例如执行了多少查询,每次执行了多少时间等等它还打印出信号。我不在我的应用程序中使用信号,所以我从未使用过该功能,但它就在那里。
#3
7
I wrote little command that shows all signal listeners: https://gist.github.com/1264102
我写了一个显示所有信号监听器的小命令:https://gist.github.com/1264102
You can modify it to show signals only.
您可以修改它以仅显示信号。
#4
0
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:
如果您只想列出特定型号上特定信号的连接接收器,可以查看_live_receivers。例如,如果要列出名为MyModel的模型的已连接post_save挂钩,则可以执行以下操作:
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
我通过查找has_listeners如何工作在Django源代码中找到了这种方法:https://github.com/django/django/blob/3eb679a86956d9eedf24492f0002de002f7180f5/django/dispatch/dispatcher.py#L153