In [38]: Device.objects.datetimes('latest_alarm_time', 'month')
Out[38]: 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
然后报错
1
ValueError: Database returned an invalid datetime value. Are time zone definitions for your database installed?
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.
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