Is there a way to list Django signals?

前端 未结 4 483
面向向阳花
面向向阳花 2021-02-04 00:48

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

4条回答
  •  Happy的楠姐
    2021-02-04 01:23

    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

提交回复
热议问题