Using QuerySet.update() versus ModelInstance.save() in Django

后端 未结 2 580
情深已故
情深已故 2021-02-04 21:30

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 ModelI

2条回答
  •  盖世英雄少女心
    2021-02-04 21:59

    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.

提交回复
热议问题