数据库 – Django – 模型save()方法是否懒惰?
发布时间:2020-12-12 07:28:50 所属栏目:MsSql教程 来源:网络整理
导读:django中的模型save()方法是否很懒? 例如,以下代码示例中的哪一行将django命中数据库? my_model = MyModel()my_model.name = 'Jeff Atwood'my_model.save()# Some code that is independent of my_model...model_id = model_instance.idprint (model_id) 解
django中的模型save()方法是否很懒?
例如,以下代码示例中的哪一行将django命中数据库? my_model = MyModel() my_model.name = 'Jeff Atwood' my_model.save() # Some code that is independent of my_model... model_id = model_instance.id print (model_id) 解决方法有一个懒惰的保存没有多大意义,是吗? Django的QuerySets 很懒,模型的保存方法不是.
来自django来源: django / db / models / base.py,第424-437行: 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) save.alters_data = True 然后,save_base执行繁重的工作(相同的文件,第439-545行): ... transaction.commit_unless_managed(using=using) ... 在django / db / transaction.py,第167-178行,你会发现: def commit_unless_managed(using=None): """ Commits changes if the system is not in managed transaction mode. """ ... 附:所有行号都适用于django版本(1,3,’alpha’,0). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |