简体   繁体   English

如何检查 Django Signal 是否有效?

[英]How to check if Django Signal works?

I have a model that I want to implement a signal's post_save on.我有一个模型,我想在其上实现信号的 post_save。 Here's my model:这是我的模型:

    class Answer(models.Model):
        question = models.ForeignKey(
            Question,
            related_name='answers_list',
            on_delete=models.CASCADE
        )

        answer = models.CharField(max_length=500)
        additional = models.CharField(
            max_length=1000,
            null=True,
            blank=True,
            default=None
        )
        created_at = models.DateTimeField(auto_now_add=True)

In order to catch the creation of the object, I've created a new signals.py file with the following context:为了捕捉对象的创建,我使用以下上下文创建了一个新的signals.py 文件:

    from django.dispatch import receiver
    from django.db.models.signals import post_save

    from core.models import Answer

    @receiver(post_save, sender=Answer)
    def answer_question(sender, instance, **kwargs):
        print("CAUGHT A SIGNAL")
        print(instance.question.sentence, instance.answer)

But when I create a new answer it doesn't seem to trigger the signal (I'm creating it via front-end, since I'm using django-rest-framework).但是当我创建一个新答案时,它似乎没有触发信号(我是通过前端创建的,因为我使用的是 django-rest-framework)。 I don't see anything in my console other than the POST method that's going to my API.除了要发送到我的 API 的 POST 方法之外,我在控制台中看不到任何内容。

What's wrong?怎么了?

We need to register our signal from AppConfig.ready() function.我们需要从AppConfig.ready()函数注册我们的信号。 link and here we need to connect our signal. 链接,在这里我们需要连接我们的信号。 Hope this will solve your problem.希望这能解决您的问题。 Just one more additional thing, if your app isn't part of INSTALLED_APPS don't forget to add init .py还有一件事,如果您的应用程序不是INSTALLED_APPS一部分,请不要忘记添加init .py

I would like to add an additional link , Please go through the section Where Should the Code Live?我想添加一个额外的链接,请阅读Where Should the Code Live? will remove your doubt.将消除你的疑虑。

Django-debug-toolbar helps to get more infos about your signals. Django-debug-toolbar 有助于获取有关信号的更多信息。 Just declare them in your settings.py:只需在 settings.py 中声明它们:

DEBUG_TOOLBAR_CONFIG = {
    'EXTRA_SIGNALS': [
        'myapp.signals.my_signal1',
        'myapp.signals.my_signal2',
    ],
}

There's a panel showing all the signals declared to dj-toolbar and their receivers.有一个面板显示声明到 dj-toolbar 的所有信号及其接收器。

You have to override ready method of application configuration class.您必须覆盖应用程序配置类的就绪方法。

In apps.py application configuration class will be defined.在 apps.py 应用程序配置类将被定义。 Overide ready method as follows覆盖就绪方法如下

from django.apps import AppConfig
class YourAppConfig(AppConfig):
    name = 'your_app'

    def ready(self):
        import your_app.signal

In project settings.py update installed apps as follows:在项目 settings.py 中更新已安装的应用程序,如下所示:

Replace代替

INSTALLED_APPS = (
 '...',
 'your_app',
)

with

INSTALLED_APPS = (
 '...',
 'your_app.apps.YourAppConfig',
)

As per django 3.0 documentation there is no need to add default_app_config in init.py https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready根据 django 3.0 文档,无需在 init.py https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready 中添加 default_app_config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM