使用 SQLite 的 Jython 中日期时间的 Django unicode 问题 JDBC

Django unicode issue with datetime in Jython with SQLite JDBC

我最近使用 Jython 开始了一个 Django 项目。我创建了一个 virtualenv 并成功创建了一个 Django 1.8.6 项目,使用 SQLite 的 JDBC 文件 (sqlite-jdbc-3.8.11.2).

我可以使用 jython manage.py createsuperuser 创建超级用户,加载管理员 URL 并成功登录。我什至可以从管理员创建另一个用户。当我尝试编辑用户时,问题就开始了。服务器无法渲染用户模型的详细实例模板。

我没有安装任何外部应用程序,也没有在项目中创建应用程序。我正在为用户模型和 Django 管理默认界面使用 django.contrib.auth。

加载 /admin/auth/user/1/ 时出现的初始错误是 'unicode' object has no attribute 'tzinfo':

AttributeError at /admin/auth/user/1/

'unicode' object has no attribute 'tzinfo'

Request Method: GET
Request URL: http://localhost:8000/admin/auth/user/1/
Django Version: 1.8.6
Exception Type: AttributeError
Exception Value:

'unicode' object has no attribute 'tzinfo'

Exception Location: /home/dmunoz/jsnmp/Lib/site-packages/django/utils/timezone.py in is_aware, line 337
Python Executable: /home/dmunoz/jsnmp/bin/jython
Python Version: 2.7.0
Python Path:

['/home/dmunoz/snmp/webswitcher',
 '/home/dmunoz/jsnmp/Lib/site-packages/django_jython-1.8.0b3-py2.7.egg',
 '/home/dmunoz/jsnmp/Lib/site-packages',
 '/home/dmunoz/jsnmp/Lib',
 '/home/dmunoz/opt/jython2.7.0/Lib',
 '__classpath__',
 '__pyclasspath__/']

Server time: Tue, 10 Nov 2015 12:31:09 -0300

Error during template rendering

In template /home/dmunoz/jsnmp/Lib/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19

因此,我尝试在 settings.py 文件中设置 USE_TZ = False。然后,我得到 'unicode' object has no attribute 'date':

AttributeError at /admin/auth/user/1/

'unicode' object has no attribute 'date'

Request Method: GET
Request URL: http://localhost:8000/admin/auth/user/1/
Django Version: 1.8.6
Exception Type: AttributeError
Exception Value:

'unicode' object has no attribute 'date'

Exception Location: /home/dmunoz/jsnmp/Lib/site-packages/django/forms/widgets.py in decompress, line 888
Python Executable: /home/dmunoz/jsnmp/bin/jython
Python Version: 2.7.0
Python Path:

['/home/dmunoz/snmp/webswitcher',
 '/home/dmunoz/jsnmp/Lib/site-packages/django_jython-1.8.0b3-py2.7.egg',
 '/home/dmunoz/jsnmp/Lib/site-packages',
 '/home/dmunoz/jsnmp/Lib',
 '/home/dmunoz/opt/jython2.7.0/Lib',
 '__classpath__',
 '__pyclasspath__/']

Server time: Tue, 10 Nov 2015 12:28:18 -0300

Error during template rendering

In template /home/dmunoz/jsnmp/Lib/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19

我遇到的最后一个错误是当我尝试使用 jython manage.py dumpdata auth.User --indent=4 --traceback 从 auth.User 模型转储数据时:

[Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/management/commands/dumpdata.py", line 159, in handle
    serializers.serialize(format, get_objects(), indent=indent,
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/serializers/__init__.py", line 129, in serialize
    s.serialize(queryset, **options)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/serializers/base.py", line 61, in serialize
    self.handle_field(obj, field)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/core/serializers/python.py", line 55, in handle_field
    self._current[field.name] = field.value_to_string(obj)
  File "/home/dmunoz/jsnmp/Lib/site-packages/django/db/models/fields/__init__.py", line 1487, in value_to_string
    return '' if val is None else val.isoformat()
AttributeError: 'unicode' object has no attribute 'isoformat'

Python 版本: 2.7.0, 杰通版本:2.7.0, Django 版本:1.8.6, django-jython 版本:1.8.0b3, SQLITE JDBC 版本:3.8.11.2


编辑:

我用一个简单的模型创建了一个 Django 应用程序:

from django.db import models

class PMV(models.Model):
    creation_date = models.DateField()

我在管理网页中创建了一个实例,日期为今天。当我试图编辑它时,我得到了这个:

在此之后,我向PMV模型添加了一个models.DateTimeField(),创建了一个实例并尝试对其进行编辑。我再次遇到 'unicode' object has no attribute 'tzinfo' 错误。

我认为这很容易解释:

  1. sqlite3 lacks explicit datetime support as 'bobince' points out in the
  2. django.db.backends.sqlite3 通过以 ISO-8601 格式存储为文本来解决这个问题
  3. 但这不是 JDBC 支持的一部分,它需要真正的日期时间支持

请注意 outstanding bug 可以为 Jython 添加完整的 sqlite3 支持。我刚刚将该错误的里程碑调整为 2.7.2(可能会更晚)。这种支持是我们一直想做的事情,虽然有点复杂,因为它不仅仅是使用 JDBC.

的问题

目前,这可能意味着您要么自行解决(不知道范围);要么或者切换到像 MySQL 或 Apache Derby 这样的数据库(像 sqlite3 一样占用空间小,但支持日期时间)。