Skip to content

Timestamped Model date_updated not updating #2

@AlexGeorgopoulos

Description

@AlexGeorgopoulos

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 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions