【问题标题】:Django unicode issue with datetime in Jython with SQLite JDBC带有 SQLite JDBC 的 Jython 中的日期时间的 Django unicode 问题
【发布时间】:2015-11-10 19:23:01
【问题描述】:

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

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

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

我在加载/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, Jython 版本: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' 错误。

【问题讨论】:

  • 看起来您有一个字符串 (unicode) 对象,其中需要一个 datetime.datetime 对象。
  • 你建议我做什么?因为默认的 Django 管理模型会发生此错误。我应该尝试使用另一个 JDBC,覆盖 auth.User 模型,还是尝试不同的方法?
  • 它确实闻起来像数据访问层中的某种错误,可能与 SQLite 实际上没有本机日期时间存储类型的方式有关。
  • 我会先尝试其他版本的 Django,甚至可能是开发快照。这可以告诉您是否有疏忽,或者是否确实存在在特定时间引入 Django 的错误。

标签: django sqlite jdbc unicode jython


【解决方案1】:

我认为这很容易解释:

  1. sqlite3 lacks explicit datetime 支持comments above 中的“bobince”指出
  2. django.db.backends.sqlite3 通过以 ISO-8601 格式存储为文本来解决此问题
  3. 但这不是 JDBC 支持的一部分,它需要真正的日期时间支持

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

目前,这可能意味着您要么自己解决问题(不知道范围);要么或切换到 MySQL 或 Apache Derby 之类的数据库(如 sqlite3 之类的小型数据库,但支持日期时间)。

【讨论】:

    猜你喜欢
    • 2012-02-02
    • 2010-10-25
    • 2011-12-27
    • 2010-12-03
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多