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?
No such thing by default, but adding one is super-easy. Just use the
auto_now_add parameter in the
created = models.DateTimeField(auto_now_add=True)
You can also use
auto_now for an ‘updated on’ field.
Check the behavior of
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