gpt4 book ai didi

python - Django 信号不起作用

转载 作者:太空狗 更新时间:2023-10-29 22:18:57 27 4
gpt4 key购买 nike

我的 models.py:>

class Aval(models.Model):
cliente = models.ForeignKey(Cliente)
salao = models.ForeignKey(Salao)
rate = models.IntegerField(choices=RATE, default=5)
criacao = models.DateTimeField(blank=True, null=True, auto_now=True)
comentario = models.TextField(max_length=400, blank=True, null=True, default=None)
aprovado = models.BooleanField(default=False)

我的signals.py:>

@receiver(post_save, sender=Aval)
def new_rate(sender, instance, created, **kwargs):
aval = instance

print("Aval is saved.")

我正在测试 Aval 模型的信号 post_save,当我保存一些对象 Aval 时它不打印“Aval is saved”。我做错了什么?

最佳答案

Django < 1.7 的原始答案:

你应该包括:

import signals

到您的应用程序的 __init__.py 文件。

编辑:Django >= 1.7:

信号可以在django.apps.AppConfig.ready中注册
如信号中所述 documentationAppConfig doc

Where should this code live?

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.

In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, import the signals submodule inside ready().

关于python - Django 信号不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com