调用模型的保存是否有区别:
self.model.save(*args,**kwargs)
和:
self.model.save()
明显的区别是,在第一种情况下,您传递的是位置和关键字参数。如果您想知道参数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()
不带参数地调用当您覆盖save
Model 类中的方法时,您肯定希望在调用基类时接受并save
传递相同的参数,即:
类 MyModel(models.Model): def save(self, *args, **kw): do_something_here() super(MyModel, self).save(*args, **kw) do_something_else()