django使用使用date_hierarchy出错(Mysql)

在使用Django的date_hierarchy功能时遇到ValueError,问题只出现在Mysql数据库中,而Postgres没有问题。错误源于Django 1.6引入的时区功能改变。解决方案是需要在Mysql中加载timezone表格,参考链接提供了解决步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



ValueError at /admin/library/questiontype/

Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
Request Method:GET
Request URL:http://127.0.0.1:8000/admin/library/questiontype/?add_time__year=2014
Django Version:1.6.2
Exception Type:ValueError
Exception Value:
Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
Exception Location:F:\Python27\lib\site-packages\django\db\models\sql\compiler.py in results_iter, line 1108
Python Executable:F:\Python27\python.exe
Python Version:2.7.5
Python Path:
['C:\\Users\\Administrator\\Desktop\\mysiteMysql',
 'F:\\Python27\\lib\\site-packages\\setuptools-3.4.4-py2.7.egg',
 'F:\\Python27\\lib\\site-packages\\pip-1.5.4-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'F:\\Python27\\DLLs',
 'F:\\Python27\\lib',
 'F:\\Python27\\lib\\plat-win',
 'F:\\Python27\\lib\\lib-tk',
 'F:\\Python27',
 'F:\\Python27\\lib\\site-packages']
Server time:星期五, 18 四月 2014 21:07:34 +0800


然后我测试过postgres,没有问题。Mysql是因为要时区不对。


在stackoverflow看到解决方法:


Looks like error caused Django's 1.6 timezone functionality changes(https://docs.djangoproject.com/en/1.6/ref/databases/#time-zone-definitions). More informative error message was recently added but only in a development version (https://code.djangoproject.com/ticket/21629).

You have to load timezone tables into mysql (http://dev.mysql.com/doc/refman/5.6/en/mysql-tzinfo-to-sql.html). Try execute on your database server:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值