使用 pre_delete 时如何访问对象的字段并对其进行操作?

How to access an object's fields and manipulate them when using pre_delete?

我在模型中有一个外键,我想在调用 pre_delete 时更改该对象中的字段值。我是这个概念的新手,刚刚发现您使用这样的预删除信号:

@receiver(pre_delete, sender=MyModel)
def bid_deletion(sender, instance, using, **kwargs):
    pass

使用外键对象的字段应该写什么?

您可以使用 instance,因此:

@receiver(pre_delete, sender=MyModel)
def bid_deletion(sender, instance, using, **kwargs):
    item = instance.<em>some_foreignkey</em>
    item.<em>field</em> = <em>some_value</em>
    item.save()