Django:如何判断post_save信号是否在新对象上触发? [英] Django: how can I tell if the post_save signal triggers on a new object?
问题描述
我需要对Django中新创建的对象进行一些后台后处理。此后处理应仅在新对象上运行,而不是在刚刚更新的对象上运行。
I need to do some background post-processing on newly created objects in Django. This post-processing should only run on new objects, not objects that are just updated.
我知道在pre_save中,我可以检查对象是否具有ID,是否存在ID那时还不是一个新对象。但是问题在于,在后期处理中,我需要访问id(以便将结果保存回数据库)。
I know that in pre_save I can check if the object has an id, if it has not then it's a new object. But the problem is that in the post-processing I need access to the id (so that I can save the results back to the database).
我该怎么做
推荐答案
看看文档: https://docs.djangoproject.com/en/stable/ref/signals/#post-save
有一个创建的
命名参数,如果设置为 True
这是一个新对象。
There is a created
named argument which will be set to True
if it's a new object.
这篇关于Django:如何判断post_save信号是否在新对象上触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!