Database returned an invalid value in QuerySet.datetimes()

Problem

When accessing the Django admin pages, you encounter a server error like this:

ValueError at /admin/dark_lang/darklangconfig/

Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?

With a traceback like this:

Traceback:
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  164.                 response = response.render()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/response.py" in render
  158.             self.content = self.rendered_content
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  135.         content = template.render(context, self._request)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/backends/django.py" in render
  74.         return self.template.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  210.                     return self._render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  135.         return compiled_parent._render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  135.         return compiled_parent._render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py" in render
  1273.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in date_hierarchy
  388.                 } for month in months]
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  162.         self._fetch_all()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  965.             self._result_cache = list(self.iterator())
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  1217.             for row in compiler.results_iter():
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  800.                     row = self.apply_converters(row, converters)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in apply_converters
  784.                 value = converter(value, expression, self.connection, self.query.context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/expressions.py" in convert_value
  927.                     "Database returned an invalid value in QuerySet.datetimes(). "

Exception Type: ValueError at /admin/dark_lang/darklangconfig/
Exception Value: Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?



Solution

The MySQL database has tables of timezone information that seems to be missing. You can re-populate the database with:

mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | mysql -u root mysql

You will need to provide the MySQL root password.