1

在我看来,好像 post_save 信号不是在整个保存过程在数据库中完成时触发的,而是在 model.save() 调用结束时触发的。

我在执行以下代码时做了这个观察:

模型.py

class PurchaseOrder(models.Model):
    total_value = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)

def purchase_order_post_save_handler(sender, instance, **kwargs):
    project_po_value_update_thread = accounting.threads.ProjectPoValueUpdateThread(instance)
    project_po_value_update_thread.start() 

post_save.connect(purchase_order_post_save_handler, sender=PurchaseOrder) 

线程.py

class ProjectPoValueUpdateThread(threading.Thread):
    """
    thread to update the po.project with the latest po values
    called via the save method of the purchase order
    """

    def __init__(self, purchase_order):
        self.purchase_order = purchase_order
        threading.Thread.__init__(self)

    def run(self):
        "starts the thread to update project po values"
        try:
            project = self.purchase_order.project
            #update active po budget
            active_po_sum = PurchaseOrder.objects.filter(project=project, is_active=True).aggregate(total_value_sum=Sum('total_value'))
            active_total_value_sum = active_po_sum['total_value_sum']
            project.total_value = active_total_value_sum
            project.save()

在某些情况下,此代码没有正确更新项目 total_value,因为使用 PurchaseOrder.objects.filter(project=project, is_active=True) 查询的实例(我刚刚保存)显然没有更新。因此,在我看来,线程已经超越了实例保存方法并查询了旧版本的模型。

我知道如何克服这个特定问题(只需从 post_save 信号提供的实例中获取最新值),但我想知道如何创建一个在数据库中的保存操作完成时触发的 post_save 信号。

4

1 回答 1

1

你是对的, post_save 信号在 save() 方法结束时执行。这个事实在Django 文档中:

django.db.models.signals.post_save

与 pre_save 类似,但在 save() 方法结束时发送。

如果您的意图是在多个领域使用信号,您可能更适合编写自己的.

编辑:更好地链接到信号定义

于 2013-03-13T12:27:42.043 回答