Issue
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 to get this automagically?
Solution
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)
Answered By – MoshiBin
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0