本文介绍了没有调用post_save信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有相关问题。



我有两个Django项目,信号在一个工作正常,但不工作在第二个(我'我只是复制粘贴的代码和更改的名称)。



我有一个订单应用程序与订单模型。应用程序包含在INSTALLED_APPS设置中。



我在apps.py中有应用配置:

 code> from django.apps import AppConfig 


class OrdersConfig(AppConfig):
name ='orders'

def ready )
super(OrdersConfig,self).ready()

#noinspection PyUnresolvedReferences
导入信号

__ init __。py

  default_app_config ='orders.apps.OrdersConfig'

最后,signals.py: / p>

  @receiver(post_save,sender = Order)
def order_save(sender,instance,created,** kwargs)
print'Post save'
if created:
print'Created'
send_email_new_order.delay(settings.MODERATOR_EMAIL,instance.pk)

而且信号没有被调用。为什么?



Django 1.10.3。

解决方案

什么时候post_save被解雇?



文档说明:在保存方法结束时。



真正的意思是:在保存方法成功完成后结束。



什么时候信号不被触发?




  1. 如果保存方法没有成功保存对象(例如当 IntegrityError 发生时)

  2. 当您调用 MyModel.objects.update()

  3. 当您覆盖 save 方法,忘记调用超类方法。

  4. 您的信号接收器尚未成功注册。



如何注册接收器



最简单的是使用 @receiver 装饰器。另一种方法是使用

  from django.db.models.signals import pre_save 

pre_save.connect (order_save,sender ='app_label.MyModel')



该代码应放在哪里?



如今,说:

所以如果你有麻烦得到您的信号接收器注册,您实际上可以尝试将代码放在 models.py views.py 中,并将其退出来自AppConfig的位(甚至可以完全删除AppConfig)



如果要在AppConfig中执行注册,而您在 @reciever 和/或导入,您可以尝试从django.db.models.signals导入pre_save $

  b $ b from app_label.signals import my_reciever 

def ready(self):
pre_save.connect(my_reciever,sender ='app_label.MyModel')



如何避免重复?



信号是否被触发两次?确保您只接收一次接收器。如果您在 AppConfig 中注册,请将其从 models.py 和副本


I've already read all related questions.

I have two Django projects, and signals work fine in one, but do not work in second one (I've just copy-pasted code and changed names respectively).

I have an orders app with Order model. App is included in INSTALLED_APPS setting.

I have app config in apps.py:

from django.apps import AppConfig


class OrdersConfig(AppConfig):
    name = 'orders'

    def ready(self):
        super(OrdersConfig, self).ready()

        # noinspection PyUnresolvedReferences
        import signals

__init__.py:

default_app_config = 'orders.apps.OrdersConfig'

And, finally, signals.py:

@receiver(post_save, sender=Order)
def order_save(sender, instance, created, **kwargs):
    print 'Post save'
    if created:
        print 'Created'
        send_email_new_order.delay(settings.MODERATOR_EMAIL, instance.pk)

And signal does not getting called. Why?

Django 1.10.3.

解决方案

When would post_save be fired?

What the document says: At the end of the save method.

What it really means: At the end of the successful completion of the save method.

When would the signal not be fired?

  1. If the save method does not successfully save the object (such as when an IntegrityError occurs)
  2. When you call MyModel.objects.update()
  3. When you override the save method and forget to call the superclass method.
  4. When your signal receiver hasn't been successfully registered.

How to register the receiver

Simplest is to use the @receiver decorator as you have done. The alternative is to use

from django.db.models.signals import pre_save

pre_save.connect(order_save, sender='app_label.MyModel')

Where should this code be placed?

Nowadays, the manual states that

That is probably why in this instance you have created a file called signals.py and place your code inside that and gone to all that trouble with the AppConfig class and the ready method. But funnily enough, the Django 1.6 manual says:

So if you are having trouble getting your signal receiver registered you can actually try putting your code in models.py or views.py and leave out the bits from AppConfig (maybe even remove AppConfig completely)

If you want to carry out the registration in AppConfig, and you are having trouble with @reciever and/or imports, you can try

from django.db.models.signals import pre_save
from app_label.signals import my_reciever

def ready(self):
    pre_save.connect(my_reciever, sender='app_label.MyModel')

How to avoid repeats?

Does the signal get fired twice? Make sure that you register the receiver only once. If you register it in AppConfig, leave it out of models.py and vice verce

这篇关于没有调用post_save信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:56