Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamped Model date_updated not updating #2

Open
AlexGeorgopoulos opened this issue Aug 3, 2020 · 0 comments
Open

Timestamped Model date_updated not updating #2

AlexGeorgopoulos opened this issue Aug 3, 2020 · 0 comments

Comments

@AlexGeorgopoulos
Copy link

AlexGeorgopoulos commented Aug 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant