如何在peewee中获取DateTimeField的值 [英] How to get the value of a DateTimeField in peewee

查看:332
本文介绍了如何在peewee中获取DateTimeField的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Test(Model):
    time = DateTimeField()

# ...
row = Test.select()[0]
test.time

这将返回一个如下所示的字符串:2017-01-23 01:01:39+01:00.我怎样才能把它作为一个日期时间对象呢?我必须手动解析吗?

This returns a string that looks like this: 2017-01-23 01:01:39+01:00. How can I get it as a datetime object instead? Do I have to parse it manually?

如果有任何关于如何使用 DateTimeField 的文档,我也会感兴趣.官方文档上没有任何内容.

Also I would be interested if there is any documentation on how to use the DateTimeField. The official documentation doesn't have anything on it.

推荐答案

您在使用 SQLite 吗?如果是这样,SQLite 没有专用的日期时间类型,因此日期时间在数据库中存储为字符串.peewee 会做的是识别来自数据库的某些日期时间格式并将它们转换为日期时间对象.您需要做的是确保:

Are you using SQLite? If so, SQLite doesn't have a dedicated datetime type, so datetimes are stored as strings in the DB. What peewee will do is recognize certain datetime formats coming out of the DB and convert them to datetime objects. What you need to do is ensure that either:

  1. 当您创建/保存对象时,您将一个 datetime 对象分配给该字段.
  2. 在读回预先存在的数据时,数据采用可识别的格式.

peewee 支持开箱即用的日期时间字段格式有:

The formats peewee supports out-of-the-box for datetime field are:

  • YYYY-mm-dd HH:MM:SS.ffffff
  • YYYY-mm-dd HH:MM:SS
  • YYYY-mm-dd

看起来您有区域信息.我建议转换为 UTC 并删除区域信息.那应该可以解决它.

It looks like your has zone info. I'd suggest converting to UTC and dropping the zone info. That should fix it.

这篇关于如何在peewee中获取DateTimeField的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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