In django - is there a default timestamp field for all objects? That is, do I have to explicitly declare a \'timestamp\' field for \'created on\' in my Model - or is there a way
No such thing by default, but adding one is super-easy. Just use the auto_now_add
parameter in the DateTimeField
class:
created = models.DateTimeField(auto_now_add=True)
You can also use auto_now
for an 'updated on' field.
Check the behavior of auto_now
here.
For auto_now_add
here.
A model with both fields will look like this:
class MyModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)