Table of Content
- auto_now vs auto_now_add in Django Date Time Fields
- Difference Between auto_now vs auto_now_add in Django
- Wrap Off
There are two useful properties that can be used
These properties are responsible for automatically managing date and time.
They are useful in keeping track of when a specific instance is created or updated.
This saves you a lot of extra code and time taken to do this manually.
All you need to do to automatically set the date and time field is to simply set the
auto_now_add arguments to
Check out the following example:
class RegisterBooklet(models.Model): description = models.CharField(max_length=255) status = models.CharField(max_length=10) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
Difference Between auto_now vs auto_now_add in Django
auto_now_add will set the
timezone.now() only when the instance is created.
On the other hand, 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
This means that when you create an object for the first time, both
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
There you have it! I hope you found this post useful somehow.
If you have any questions or need clarification, feel free to reach out to me.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.