我有一个代码,像这样
def many_objects_saving(list_of_objects):
for some_object in list_of_objects:
# do smth with an object
some_object.save()
据我所知,django 每次在代码中到达 save() 时都会访问数据库。所以这里有两个问题:
- 如果我将在另一个函数中使用这个函数并用
transaction.commit_on_success
或transaction.commit_manually
装饰器包装它,django 会在一个事务中完成所有保存并更少地访问数据库(在一个函数内)吗?
例子:
def resave_objects(model, condition):
list_of_objects = model.objects.filter(**condition)
many_objects_save(list_of_objects)
@transaction.commit_on_success
def many_objects_save(list_of_objects):
for some_object in list_of_objects:
# do smth with an object
some_object.save()
2. 如果是这样,对于大型查询集会更好吗?谢谢你们!