[Fixed]-Why is a auto_now=True field designed to not update when using QuerySet.update()?

1đź‘Ť

Update query is meant to be faster than the regular field changing and saving pattern, thus it does not call the save() method, which handles updating auto_now fields, sending signals and so on. If you’re not sure what you’re doing, then it’s always a good idea to explicitly call the save() on a model. Advanced and “less restricted” methods such as update or bulk_create are faster and meant for editing data on DB level. From Django docs:

Finally, realize that update() does an update at the SQL level and,
thus, does not call any save() methods on your models, nor does it
emit the pre_save or post_save signals (which are a consequence of
calling Model.save()).

If you were hoping for a more technical explanation, then the update query probably doesn’t bother to check if the table has an auto_now field. It would require some data gathering and make the process slower. If you do want to update the field, you can update it explicitly.

👤wanaryytel

Leave a comment