0

我有一个长期运行的过程,它依赖于模型字段。

假设我有一个看起来像这样的模型

class Client(models.Model):
    input_data = models.CharField(max_length=100)

    # The field that will be computed in the background task
    computed_data = models.CharField(max_length=100, null=True, blank=True)

我想每次更新这个模型时触发一个后台任务,但问题是我还需要在后台任务更新实例后调用save方法。

它大致看起来像这样。

def a_long_running_background_task(instance):
    input_data = instance.input_data # Reading the model input data
    
    # Updating the output_data field using the input data from the model instance    
    instance.output_data = input_data.title()

    # To make sure the changes apply to the database we need to call the save method
    instance.save()

这段代码的问题是它会导致无限循环,因为我在save方法上调用了后台任务,而我在后台任务中调用了save方法。

我一直收到这个错误

RecursionError: maximum recursion depth exceeded while calling a Python object

在我研究了这个问题之后,我找到了一个解决方案,它建议使用带有 created 属性的 post_save 信号,然后检查实例是否已创建,然后执行任务,如果它刚刚更新,我会跳过它,因为这意味着它正在从后台工作人员调用保存。

信号看起来像这样:

@receiver(post_save, sender=Client)
def handle_new_client(sender, instance, created, **kwargs):
    if created:
        a_long_running_task(instance)

现在的问题是,我想要一个更新功能,并且我希望在更新模型时触发后台任务,但它目前仅在创建对象时触发。

我有一个解决方案,但我不确定如何实现它或者它是否可行,即将计算字段从输入字段拆分为两个不同的模型。

非常感谢任何帮助。

4

1 回答 1

0

一种选择是使用bulk_update更新后台任务中的实例。这个函数不会调用模型的save方法,也不会发出任何信号,消除了任何递归问题。

于 2021-02-07T20:01:34.507 回答