You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a model instance is created, date_created, date_updated are correctly set to timezone.now().
However, when this instance is updated, date_updated is not updating.
I think this is expected since the column in TimeStamped Model is: date_updated = models.DateTimeField( _('date updated'), default=timezone.now)
which dictates a default value when there is none. So, when there is a value already, there will be no update.
What I had done to fix the issue was to create a custom Timestamped Model with the columns below: date_created = models.DateTimeField(_('date_created'), default=timezone.now) date_updated = models.DateTimeField(_('date_updated'), auto_now=True)
One thing to take into consideration is that I used default=timezone.now, in order to be able to override the date_created if needed. If you want the date_created value to be irreversible, then you should use auto_now_add=True, where the default value cannot be overridden (same goes with auto_now=True).
The text was updated successfully, but these errors were encountered:
Hello!
Django version 2.2.1 used.
When a model instance is created,
date_created
,date_updated
are correctly set to timezone.now().However, when this instance is updated,
date_updated
is not updating.I think this is expected since the column in TimeStamped Model is:
date_updated = models.DateTimeField( _('date updated'), default=timezone.now)
which dictates a default value when there is none. So, when there is a value already, there will be no update.
What I had done to fix the issue was to create a custom Timestamped Model with the columns below:
date_created = models.DateTimeField(_('date_created'), default=timezone.now)
date_updated = models.DateTimeField(_('date_updated'), auto_now=True)
One thing to take into consideration is that I used
default=timezone.now
, in order to be able to override thedate_created
if needed. If you want thedate_created
value to be irreversible, then you should useauto_now_add=True
, where the default value cannot be overridden (same goes withauto_now=True
).The text was updated successfully, but these errors were encountered: