让 django 信号在应用程序之间工作
Getting django signals to work between applications
我有一个名为 receipt_printer 的 Django 应用程序,它应该在使用信号接收到订单时打印订单。这里有两个应用程序,名为:Orders 和 receipt_printer.
但是,无论何时处理订单并充满电,我都无法使用信号。为了简单起见,下面的示例只是将一些内容打印到控制台。如果我在 Orders 应用程序的 models.py 中执行所有操作,它就可以正常工作。
receipt_printer/apps.py
from django.apps import AppConfig
class ReceiptPrinterConfig(AppConfig):
name = 'receipt_printer'
def ready(self):
from . import signals
receipt_printer/signals.py
from django.db import models
from saleor.order.models import Order
from django.db.models.signals import post_save
def order_fully_paid_signal(sender, instance, created, **kwargs):
if instance.get_payment_status() == "fully-charged":
print("This signal is working for fully paid order")
else:
print("This signal won't working for fully paid order")
post_save.connect(order_fully_paid_signal, sender=Order)
receipt_printer/init.py
default_app_config = 'receipt_printer.apps.ReceiptPrinterConfig'
更新,也尝试了以下 - 没有用:
@receiver(post_save, sender=Order)
def order_fully_paid_signal(sender, instance, created, **kwargs):
if instance.get_payment_status() == "fully-charged":
print("This signal is working for fully paid order")
else:
print("This signal won't working for fully paid order")
不完全确定这里的问题是什么,但下面是我使用的 post_save 模式,它可以跨应用工作,无需对 apps.py 或 init.py 进行任何更改:
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=<Model>, dispatch_uid='<model>_post_save')
def <model>_post_save(sender, instance, **kwargs):
...
我需要将以下行添加到订单应用程序 models.py 的底部:
from saleor.saleor_package.receipt_printer.signals import order_fully_paid_signal
我有一个名为 receipt_printer 的 Django 应用程序,它应该在使用信号接收到订单时打印订单。这里有两个应用程序,名为:Orders 和 receipt_printer.
但是,无论何时处理订单并充满电,我都无法使用信号。为了简单起见,下面的示例只是将一些内容打印到控制台。如果我在 Orders 应用程序的 models.py 中执行所有操作,它就可以正常工作。
receipt_printer/apps.py
from django.apps import AppConfig
class ReceiptPrinterConfig(AppConfig):
name = 'receipt_printer'
def ready(self):
from . import signals
receipt_printer/signals.py
from django.db import models
from saleor.order.models import Order
from django.db.models.signals import post_save
def order_fully_paid_signal(sender, instance, created, **kwargs):
if instance.get_payment_status() == "fully-charged":
print("This signal is working for fully paid order")
else:
print("This signal won't working for fully paid order")
post_save.connect(order_fully_paid_signal, sender=Order)
receipt_printer/init.py
default_app_config = 'receipt_printer.apps.ReceiptPrinterConfig'
更新,也尝试了以下 - 没有用:
@receiver(post_save, sender=Order)
def order_fully_paid_signal(sender, instance, created, **kwargs):
if instance.get_payment_status() == "fully-charged":
print("This signal is working for fully paid order")
else:
print("This signal won't working for fully paid order")
不完全确定这里的问题是什么,但下面是我使用的 post_save 模式,它可以跨应用工作,无需对 apps.py 或 init.py 进行任何更改:
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=<Model>, dispatch_uid='<model>_post_save')
def <model>_post_save(sender, instance, **kwargs):
...
我需要将以下行添加到订单应用程序 models.py 的底部:
from saleor.saleor_package.receipt_printer.signals import order_fully_paid_signal