0

我有非常标准的 save_model() 覆盖,它计算一些值并在最后调用 object.save()

我正在重写应用程序以获得更好的可扩展性,并读到 .save() 不是线程安全的,并且不允许数据分区。推荐的最佳实践是使用 update()。

我的问题是:如果我在 save_model() 中执行 update() 而不是 save() 可以吗?我看不出它有什么缺点,你呢?

4

1 回答 1

0

I think the warning in the docs about updating multiple objects at once is relevant. If you're not using any signals, overriding the model's save method, or using the auto_now field option, I can't think of any other potential problems.

Be aware that the update() method is converted directly to an SQL statement. It is a bulk operation for direct updates. It doesn't run any save() methods on your models, or emit the pre_save or post_save signals (which are a consequence of calling save()), or honor the auto_now field option. If you want to save every item in a QuerySet and make sure that the save() method is called on each instance, you don't need any special function to handle that. Just loop over them and call save():

于 2012-11-14T14:45:11.317 回答