Django 信号:使用 update_field 作为条件

Django Signals: using update_field as condition

谁能帮我理解 Django 信号的 update_field 参数?

According to the docs:

update_fields: The set of fields to update explicitly specified in the save() method. None if this argument was not used in the save() call.

我不清楚这是什么意思。我试图用它来阻止信号函数的执行,除非更新了某些字段:

@receiver(post_save, sender=SalesRecord)
def spawn_SaleSource_record(sender, update_fields, created, instance, **kwargs):
    if created or update_fields is 'sale_item' or 'sales_qty':
        *do function*

然而,当一个对象被保存时,它似乎仍然在另一个信号过程中执行,即使一个未指定的字段被显式更新:

x = SalesRecord.objects.filter(paid_off=False, customer=instance.customer).first()
x.paid_off = True
x.save(update_fields=['paid_off'])

我做错了吗?

您的条件不符合您的要求,因为 'sales_qty' 始终为真。

你希望你的条件是:

if created or 'sale_item' in update_fields or 'sales_qty' in update_fields: