when to use pre_save, save, post_save in django?

前端 未结 3 2188
南笙
南笙 2020-12-13 19:04

I see I can override or define pre_save, save, post_save to do what I want when a model instance gets saved.

Which one is preferred in which situation

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 19:17

    pre_save
    

    it's used before the transaction saves.

    post_save
    

    it's used after the transaction saves.

    You can use pre_save for example if you have a FileField or an ImageField and see if the file or the image really exists.

    You can use post_save when you have an UserProfile and you want to create a new one at the moment a new User it's created.

提交回复
热议问题