调用模型的保存之间是否有区别:
self.model.save(*args,**kwargs)
和:
self.model.save()
答案 0 :(得分:2)
明显的区别在于,在第一种情况下,您传递的是位置和关键字参数。如果你想知道Model.save()
采用什么参数以及它们做了什么,最简单的方法是阅读源代码。然后你会找到类似的东西:
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)
第三个参数using
没有记录,它指定了您要使用的数据库连接(如果您有多个数据库连接)。
简而言之:
my_model_instance.save()
当您覆盖Model类中的save
方法时,您肯定希望在调用基类save
时接受和传递相同的参数,即:
类MyModel(models.Model): def save(self,* args,** kw): do_something_here() super(MyModel,self).save(* args,** kw) do_something_else的()