Django: Before a model is updated, I'd like to “look at” its previous attributes

后端 未结 5 730
星月不相逢
星月不相逢 2021-02-13 16:36

When an update/create is performed on a Django model (.save()) I would like to be able to \"step in\" and compare some particular attributes to what they were set t

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 17:23

    While I very much approve of Sébastien Piquemal's answer I ultimately ended up using both the pre_save and post_save signals. Instead of overriding __init__(), I do something very similar in pre_save, and then check/compare the values in post_save and emit a custom signal from there if certain conditions are met.

    I think I'll still accept his answer, for the time spent on it. I don't see where we're doing much different, except I'm doing my work in a signal and he's doing it during initialization.

提交回复
热议问题