[Answered ]-Django Models auto_now saving same time as auto_now_add

1๐Ÿ‘

โœ…

TL;DR: when you create an object for the first time, both created_at and updated_at will be filled with values, accordingly to the code you crafted.

  • The auto_now_add will set the timezone.now() only when the instance is created.
  • The auto_now will update the field every time the save method is called.

However, both functions the same way by triggering the field update event with timezone.now().

This means that when you create an object for the first time, both created_at and updated_at will be filled with values.

But, the next time you save that object, the created_at will remain the same, while the updated_at will call the timezone.now().

The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

Django docs: https://docs.djangoproject.com/en/4.0/ref/models/fields/

๐Ÿ‘คCarlo

Leave a comment