django update model doesn't affect auto_now timestamp
It doesn't work as posted in this issue (declared as won't fix) https://code.djangoproject.com/ticket/22981
.update() will only update the fields explicitly passed to it, so datetimefields with auto_now =True aren't updated.
I know the documented behavior is that .update()
will ignore updating auto_now[_add]
fields as is also confirmed by Yunti's answer. However, for those who wish to force the desired effect - for example, having an auto_now
field updated as part of an .update()
invocation, the following approach seems to work...
update(field1=val1, field2=val2,..., last_update=datetime.now())
Where last_update
is a models.DateTime
type field with (or without) auto_now=True
. However, note that if you pass some other value to this field other than now()
- the current timestamp that is, the ORM doesn't help in correcting this to the current timestamp (which is what the auto_now
flag semantics imply). So, for example, in the following snippet, the last_update
field does get overridden with the specified timestamp, irrespective of whether it's past, present or future...
update(field1=val1, field2=val2,..., last_update=datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p'))
So, if you wish to enforce the standard semantics of auto_now
when making your calls to update()
, ensure to correctly pass the current timestamp as the update value to these fields. You are doing it manually (due to a flaw/deficiency in the Django ORM implementation), but you are doing it correctly thus.