1. 错误详情

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

Traceback:
File "/var/env/pyblog/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/var/env/pyblog/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/var/env/pyblog/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/env/pyblog/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/var/env/pyblog/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/var/env/pyblog/lib/python3.5/site-packages/django/views/generic/dates.py" in get
  348.         self.date_list, self.object_list, extra_context = self.get_dated_items()

File "/var/env/pyblog/lib/python3.5/site-packages/django/views/generic/dates.py" in get_dated_items
  434.         date_list = self.get_date_list(qs, ordering='DESC')

File "/var/env/pyblog/lib/python3.5/site-packages/django/views/generic/dates.py" in get_date_list
  413.         if date_list is not None and not date_list and not allow_empty:

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/query.py" in __bool__
  254.         self._fetch_all()

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1105.             self._result_cache = list(self._iterable_class(self))

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  152.         for row in compiler.results_iter():

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in results_iter
  834.                     row = self.apply_converters(row, converters)

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in apply_converters
  819.                 value = converter(value, expression, self.connection, self.query.context)

File "/var/env/pyblog/lib/python3.5/site-packages/django/db/models/functions/datetime.py" in convert_value
  198.                         "Database returned an invalid datetime value. "

2. 解决方法

2.1. Linux

使用mysql_tzinfo_to_sql命令在名为mysql的数据库中填充time_zone表:

root@ubuntu:~# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Enter password:
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2.2. Windows

  • 此页面下载时区文件
  • 将压缩包中的文件解压至MySQL安装目录下的/data/mysql/中,覆盖原有文件。