Home  >  Article  >  Backend Development  >  Bug in Django's update and save() functions at the same time

Bug in Django's update and save() functions at the same time

高洛峰
高洛峰Original
2016-11-22 14:55:161578browse

save() By default, after saving, you will see that all fields have been updated in the sql statement, and the value of save is the field value when it was obtained before. It is cached and is not necessarily the latest. It may have been updated elsewhere in the process

UPDATE `pxb_sx2_test` SET `user_id` = 335, `catalog_id` = 12558, `level_id` = 4, `level_status` = 0, `position` = 440, `type` = 2, `add_time` = '2016-09-14 17:44:07', `update_time` = '2016-09-14 17:53:17.077520' WHERE `pxb_sx2_test`.`id` = 175;

update updates the specified field

UPDATE `pxb_sx2_test` SET `position` = 441 WHERE `pxb_sx2_test`.`id` = 1751;

If both are used at the same time, or in different places, but they may be updated at the same time, there will be a chance of updating, but the value in the database will not change. In fact, after update, the outdated field value happened to be saved, so it seemed that it was not updated. After Django 1.5, save can add parameters to only update specified fields. Reference

 test.position = 441
 test.save(update_fields=['position'])


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn