问题描述
我正在使用Django的pre_save信号来实现auto_now_add。互联网上有很多关于您为什么应该或不应该自己实施的讨论。我不喜欢对此发表评论。无论我是否应该重写save函数(我有很多使用auto_now_add的模型,因此使用信号都有意义)。
I am using Django's pre_save signal to implement auto_now_add. There is a lot of discussion on the internet on why you should or shouldn't implement it yourself. I do not appreciate comments on this. Neither on whether I should be rewriting the save function (I have a lot of models that use auto_now_add so using signals makes sense).
我的问题是:
我想检查实例是创建还是更新。根据互联网上的一些消息来源,可以通过测试 kwargs [’created’]
是否为True来完成。但是,即使实例是新创建的,创建的
也不会出现在我的 kwargs
中。
我只是想知道它是否曾经存在过或者它已经神奇消失了。
我知道我也可以测试是否设置了 kwargs ['instance']。id
(实际上这对我有用),但是我想知道如果kwargs ['created']仍然存在。
My question is:
I would like to check if the instance is created or updated. According to some sources on the internet this can be done by testing if kwargs['created']
is True. However 'created'
does not appear in my kwargs
even though the instance is newly created.I was just wondering if it has ever existed or that it has disappeared magically.I know I could also test if kwargs['instance'].id
is set (this in fact works for me), but I'd like to know if kwargs['created'] still exists.
推荐答案
根据最新的Django , pre_save
不会发送创建的
参数。 Post_save
但是。自。
According to the latest Django documentation, pre_save
does NOT send a created
argument. Post_save
however does. I could not find any reference of the signal sending created
since version 1.0.
这篇关于Django pre_save信号:检查实例创建是否未更新,是否存在kwargs ['created'](仍然存在)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!