save和save_model()之间的区别 [英] Difference between save and save_model()

查看:225
本文介绍了save和save_model()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于覆盖save方法所需的所有Django需求,我一直在模型中使用 save()。当我查找需要一些额外照明的东西时,突然间有人建议在我的管理类中重写 save_model()

For all of my Django needs that overriding the save method requires, I've been using save() in my models. All the sudden when I was looking up something that required a little extra flare, someone suggested overriding save_model() in my admin class.

我在两个文档上都找到了文档,并且立即注意到参数是不同的。
仅在从管理员保存模型时才调用 save_model()吗?并在保存模型时调用 save()方法?

I found the docs on both, and I noticed right away that the arguments are different. Is save_model() only called when the model is saved from the admin? and the save() method is called whenever the model is saved?

更新

因此,我从管理员保存时已经知道了这一点。 save和 save_model()都会被调用。似乎首先调用了 save_model()。在我的特定应用程序中,我使用的是pre_save信号,并且使用 save()替代。现在,我要添加 save_model()覆盖。所有这些保存替代和信号的相互作用和顺序令我感到困惑。

So I've figured out that when saving from the admin. Both 'save' and 'save_model()' get called. It appears that 'save_model()' gets called first. In my specific application, I am using pre_save signals, and 'save()' overrides. Now I want to add in 'save_model()'overrides . It's the interaction and order of all of these save overrides and signals that is confusing me.

推荐答案

由于 save()是<$ c $的方法c> Model ,而 save_model() ModelAdmin 的方法。

As save() is a method of Model whereas save_model() is a method of ModelAdmin... yes.

这篇关于save和save_model()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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