In django do models have a default timestamp field?

前端 未结 6 1329
误落风尘
误落风尘 2021-01-30 09:53

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

6条回答
  •  长发绾君心
    2021-01-30 10:23

    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)
    

提交回复
热议问题