`after_create` 和 `after_save` 之间有什么区别以及何时使用哪个? [英] What is the difference between `after_create` and `after_save` and when to use which?

查看:42
本文介绍了`after_create` 和 `after_save` 之间有什么区别以及何时使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

after_createafter_save 是否与每个功能相同?

Are after_create and after_save the same as per functionality?

我想在用户创建帐户后对其电子邮件进行操作.

I want to do an operation with the email of a user after its account creation.

我想在它保存在数据库中时做那个操作.

I want to do that operation when it is saved in the database.

哪个更适合使用:after_createafter_save?

which is preferable to use: after_create or after_save?

推荐答案

after_create 只工作一次 - 就在第一次创建记录之后.

after_create only works once - just after the record is first created.

after_save 每次保存对象时都有效 - 即使您在多年后才更新它

after_save works every time you save the object - even if you're just updating it many years later

因此,如果您只想执行此电子邮件操作一次(然后再也不执行),请使用 after_create.

So if you want to do this email operation only just the once (and then never again) then use after_create.

如果你想每次保存对象时都这样做,那么在after_save

If you want to do it every time the object is saved, then do it in after_save

这篇关于`after_create` 和 `after_save` 之间有什么区别以及何时使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆