我一直在尝试学习如何使用Signals,不幸的是,下面的代码无法正常工作。此外,我碰到过十几篇文章,所有文章都展示了执行此操作的不同方法。
我在这篇文章中的目标是澄清并达成共识,以2019年可能的最佳方式编写信号以实现可扩展性。在此示例中,使用post_save。
model.py
from django.db import models
from metrics.models import InventoryProduct
from product.models.product import Product
class PurchasedOrder(models.Model):
__quantity_sold = None
product = models.ForeignKey(Product, on_delete=models.CASCADE)
purchase_quantity = models.IntegerField()
quantity_sold = models.IntegerField(default=0)
def __init__(self, *args, **kwargs):
super(PurchasedOrder, self).__init__(*args, **kwargs)
self.__original_quantity_sold = self.quantity_sold
def save(self, *args, **kwargs):
# If the quantity sold field has changed run the update_sold_out method
if self.quantity_sold != self.__original_quantity_sold:
PurchasedOrderLogic.update_sold_out(self)
super(PurchasedOrder, self).save(*args, **kwargs)
self.__original_quantity_sold = self.quantity_sold
def __str__(self):
return self.product.name
signals.py:
from django.db.models.signals import post_save
from django.dispatch import receiver
from purchasing.models import PurchasedOrder
from purchasing.services.models_logic import PurchasedOrderLogic
@receiver(post_save, sender=PurchasedOrder)
def update_inventory_product_total_qty_purchased(sender, instance):
PurchasedOrderLogic.update_inventory_product_total_qty_purchased(
sender, instance.product.id, instance.purchase_quantity
)
这是我的逻辑所在。当我更新PurchasedOrder Purchase_quantity字段时,它还应该更新InventoryProduct的total_qty_purchased字段。
purchasing.services.models_logic.py
class PurchasedOrderLogic(object):
@static_method
def update_inventory_product_total_qty_purchased(purchased_order_class, product_id):
inventory_product = InventoryProduct.objects.get(pk=product_id)
total_qty_purchased = purchased_order_class.objects.filter(product_id=product_id).aggregate(
Sum('purchase_quantity')
).get('purchase_quantity__sum')
inventory_product.total_qty_purchased = total_qty_purchased
inventory_product.save()
apps.py
from django.apps import AppConfig
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from purchasing.models import PurchasedOrder
from purchasing.signals import update_inventory_product_total_qty_purchased
class PurchasingConfig(AppConfig):
name = 'purchasing'
verbose_name = _('purchasing')
def ready(self):
post_save.connect(update_inventory_product_total_qty_purchased, sender=PurchasedOrder)
缺少什么?我不确定信号是否在运行?
我通过手动修改模型上的任何字段来触发.save()。
我个人一直在更改purchase_quantity字段,该字段应打印此值,因为它在update_inventory_product_total_qty_purchased方法内部被调用