【问题标题】:Peewee and SQLite returning incorrect date formatPeewee 和 SQLite 返回不正确的日期格式
【发布时间】:2018-10-11 10:34:50
【问题描述】:

我已经用 Python 和 Flask 构建了一个 Web 应用程序,但无法从我的 SQLite 数据库中提取日期和时间。

我使用以下行将日期输入数据库-
order.order_placed = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

在我当前的示例中,将以下内容输入数据库 -
2018-05-01 12:08:49

但是当我打电话给order.order_placed 我得到datetime.date(2018, 5, 1)

即使我打电话给str(order.order_placed),我也会得到'2018-05-01'

有人可以帮我从数据库中获取完整的日期和时间吗?谢谢!

【问题讨论】:

  • 你是在使用 DateField 而不是 DateTimeField 吗?
  • 请提供minimal reproducible example。如果是 Peewee 等 ORM,请提供相关模型等。请记住正确格式化代码(每行以 4 个空格开头)。

标签: python sqlite flask peewee


【解决方案1】:

您可能正在使用DateField,而实际上您想使用DateTimeField

此外,您无需在存储数据之前调用strftime。 Peewee 可以很好地与 Python datetime 对象配合使用。

【讨论】:

  • 这不是答案。
  • 谢谢大家,我在模型中使用了错误的字段类型!
猜你喜欢
  • 1970-01-01
  • 2020-11-15
  • 2020-01-06
  • 1970-01-01
  • 2019-12-03
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多