def save(self, force_insert=False, force_update=False, using=None):
"""
Saves the current instance. Override this in a subclass if you want to
control the saving process.
The 'force_insert' and 'force_update' parameters can be used to insist
that the "save" must be an SQL insert or update (or equivalent for
non-SQL backends), respectively. Normally, they should not be set.
"""
if force_insert and force_update:
raise ValueError("Cannot force both insert and updating in model saving.")
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
明显的区别在于,在第一种情况下,传递的是位置参数和关键字参数。如果您想知道
Model.save()
使用什么参数以及它们做什么,最简单的事情就是阅读源代码。然后你会发现:第三个参数
using
没有记录,它指定要使用哪个数据库连接(如果有多个数据库连接)。在长话短说:
my_model_instance.save()
当您重写模型类中的
save
方法时,您确实希望在调用基类save
时接受并传递相同的参数,即:MyModel类(模型。模型): def save(自身,*参数,**千瓦): 在这里做点什么() 超级(MyModel,self)。节省(*args,**kw) 做些什么别的什么()
相关问题 更多 >
编程相关推荐