登录
首页 >  数据库 >  MySQL

Django | Mysql 返回不合法的日期时间对象

来源:SegmentFault

时间:2023-01-28 17:57:35 371浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Django | Mysql 返回不合法的日期时间对象》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

1 错误描述

在查询数据集中的日期时间对象时

ValueError: Database returned an invalid datetime value. Are time zone definitions for your database installed?

2 解决问题

实际情况,数据库中是有数据,目测月份提取失败;到 mysql 执行了下

mysql root@localhost:py365> select convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai');
+-------------------------------------------------------------+
| convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai')   |
|-------------------------------------------------------------|
| NULL                                                        |
+-------------------------------------------------------------+

果然,结果返回令人诧异的

NULL

看了下 Django orm 的 datetimes 官方文档

Note
This function performs time zone conversions directly in the database. As a consequence, your database must be able to interpret the value of tzinfo.tzname(None). This translates into the following requirements:

SQLite: no requirements. Conversions are performed in Python with pytz (installed when you install Django).
PostgreSQL: no requirements (see Time Zones).
Oracle: no requirements (see Choosing a Time Zone File).
MySQL: load the time zone tables with mysql_tzinfo_to_sql.

即 mysql 需要使用 mysql_tzinfo_to_sql 载入时区表,接着跳到 https://dev.mysql.com/doc/ref...

按照 mysql 官方的文档

For the first invocation syntax, pass the zoneinfo directory path name to mysql_tzinfo_to_sql and send the output into the mysql program. For example:

我需要按照以下命令执行

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

然后再次执行上面执行过的转换语句

mysql root@localhost:py365> select convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai');
+-------------------------------------------------------------+
| convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai')   |
|-------------------------------------------------------------|
| 2018-05-10 20:30:00                                         |
+-------------------------------------------------------------+

yes,返回了正确的结果;

在 shell 中 执行数据库查询语句

In [45]: Device.objects.datetimes('latest_alarm_time', 'month')
Out[45]: SELECT DISTINCT CAST(DATE_FORMAT(CONVERT_TZ(`device_device`.`latest_alarm_time`, 'UTC', 'Asia/Shanghai'), '%Y-%m-01 00:00:00') AS DATETIME) AS `datetimefield` FROM `device_device` WHERE `device_device`.`latest_alarm_time` IS NOT NULL ORDER BY `datetimefield` ASC LIMIT 21


Execution time: 0.000591s [Database: default]

)]>

正常,so 问题解决,看来还得认真看文档呀

今天关于《Django | Mysql 返回不合法的日期时间对象》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表