在Django中使用QuerySet.update()和ModelInstance.save() [英] Using QuerySet.update() versus ModelInstance.save() in Django

查看:1385
本文介绍了在Django中使用QuerySet.update()和ModelInstance.save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇别人对这个问题的看法...



过去几天我一直在使用 QuerySet.update() ModelInstance.save()。显然,如果改变了很多字段,我会使用 save(),但是为了更新几个字段,我认为最好使用 QuerySet.update()。使用 QuerySet.update()的好处是,您可以同时运行多个线程 update() ,在同一个对象的不同领域,你不会有种族问题。默认的 save()方法保存所有的字段,所以从两个线程并行 save()将是有问题的。 / p>

那么问题是如果你有重载,定制的 save()方法。我可以想到的最好是将自定义的 save()方法抽象到实际使用 QuerySet.update()设置模型中的几个字段。有没有人使用过这种模式?



在Django Admin中,甚至在编辑只有一个字段的变更列表模式中编辑时,整个模型是保存。这基本上意味着如果有人在他/她的浏览器上打开了一个更改列表,而一些系统中的某个字段更新,那么当用户从更改列表中保存更改时,更新的值将被丢弃。有没有解决这个问题?



想法?



谢谢。

解决方案

使用 QuerySet.update()的主要原因是您可以更新多个对象只需一个数据库查询,而每个调用一个对象的保存方法就会打到数据库!



另外值得一提的是,django的 pre_save & post_save 信号仅在您调用对象的 save -method时发送,但不在 QuerySet .update()



对于您描述的冲突问题,我认为如果您点击保存,那么也会很刺激,你必须发现,一些值与改变它们是一样的,但是你保持不变的一些值已经改变了?当然,您可以将管理员的 save_model 或对象的保存方法修改为您建议的bahaviour。 p>

I am curious what others think about this problem...

I have been going back and forth in the past few days about using QuerySet.update() versus ModelInstance.save(). Obviously if there are lots of fields being changed, I'd use save(), but for updating a couple of fields, I think it's better to use QuerySet.update(). The benefit of using QuerySet.update() is that you can have multiple threads running update() at the same time, on different fields of the same object, and you won't have race issues. The default save() method saves all the fields, so parallel save() from two threads will be problematic.

So then the issue is what if you have overloaded, custom save() methods. The best I can think of is to abstract whatever in the custom save() method into separate updater methods that actually uses QuerySet.update() to set a couple of fields in the model. Has anyone used this pattern?

What's a bit irritating is that in Django Admin, even in editing in change list mode where you are editing just one field, the entire model is saved. This basically means if someone have a change list open on his/her browser, while some where else in the system a field gets updated, that updated value will be thrown away when this user saves changes from the change list. Is there a solution to this problem?

Thoughts?

Thanks.

解决方案

The main reason for using QuerySet.update() is that you can update more than one object with just one database query, while every call to an object's save method will hit the database!

Another point worth mentioning is, that django's pre_save & post_save signals are only sent when you call an object's save-method, but not upon QuerySet.update().

Coming to the conflict issues you describe, I think it would also be irritating if you hit 'save' and then you have to discover that afterwards some values are the same as when you changed them, but some that you left unchanged have changed?!? Of course it's up to you to modify the admin's save_model or the object's save method to the bahaviour you suggest.

这篇关于在Django中使用QuerySet.update()和ModelInstance.save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆