DateField have two very useful arguments for automatically handling date and time fields. You don’t need to keep track of creation and updation of data manually. Just set the
auto_now_add arguments to
True like in the following example:
1class BaseMode(models.Model):2 created_at = models.DateTimeField(auto_now_add=True)3 updated_at = models.DateTimeField(auto_now=True)45 class Meta:6 abstract = True
auto_now_add will set the
timezone.now() only when the instance is created, and
auto_now will update the field every time the
save method is being called on model.
And at the time of creation of instance, both will be filled.