在django中,模型有默认的时间戳记字段吗? [英] In django do models have a default timestamp field?

查看:1011
本文介绍了在django中,模型有默认的时间戳记字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django中 - 所有对象都有默认的时间戳记字段?也就是说,我必须在我的Model中明确声明'created on'的'timestamp'字段,还是有办法自动获取这个?

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?

推荐答案

默认情况下没有这样的东西,但添加一个是超级容易的。只需使用 DateTimeField 类中的 auto_now_add 参数:

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)

:您还可以使用 auto_now 来获取更新的字段。

edit: You can also use auto_now for an 'updated on' field.

这篇关于在django中,模型有默认的时间戳记字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆