【问题标题】:How do we set default update (as default = _datetime.now) for timestamp (unix epoch) in django python?我们如何在 django python 中为时间戳(unix 纪元)设置默认更新(默认 = _datetime.now)?
【发布时间】:2020-05-20 23:41:02
【问题描述】:

我希望通过 Python 中的 Django 模型获得一个自动的 Unix 纪元时间戳。 当记录输入数据库时​​,我正在尝试存储 Unix 时间戳。

【问题讨论】:

  • 这个问题缺少很多细节......

标签: python django timestamp unix-timestamp


【解决方案1】:

当记录被输入数据库时​​。

从你所说的,我认为这应该由数据库(模型)处理。

但是如果你想获取当前epoch时间并插入到数据库中,希望对你有帮助

import time
from datetime import datetime

if __name__ == '__main__':
  currenttime = time.time() #epoch time

  #print in local datetime
  print(datetime.fromtimestamp(currenttime).strftime('%Y-%m-%d %H:%M:%S'))

  #print in GMT datetime
  print(datetime.utcfromtimestamp(currenttime).strftime('%Y-%m-%d %H:%M:%S'))


  # Logic for database insertion...

【讨论】:

  • 是的,就目前而言这很好,但我希望在插入表格时更新它。
  • 您好,您能详细描述一下您的案例吗?我无法理解你的意思。对不起
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多