2015-11-10 41 views
1

我最近使用Jython啓動了一個Django項目。我創建了一個virtualenv並使用SQLite的JDBC文件(sqlite-jdbc-3.8.11.2)成功創建了一個Django 1.8.6項目。Django unicode問題Jython中的datetime與SQLite JDBC

我可以創建一個超級用戶jython manage.py createsuperuser,加載管理網址併成功登錄。我甚至可以從管理員創建另一個用戶。我試圖編輯用戶時出現問題。服務器無法呈現用戶模型的詳細實例的模板。

我沒有安裝任何外部應用程序,也沒有在項目中創建應用程序。我爲用戶模型和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, 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() 

我在管理網頁=今天創建一個實例,與日期。當我試圖編輯它,我得到這個:

Screenshot from admin, model with DateField

在此之後,我添加了一個models.DateTimeField()到PMV模型,創建一個實例,並試圖對其進行編輯。我再次得到了'unicode' object has no attribute 'tzinfo'錯誤。

+0

看起來你有一個字符串('unicode')對象,其中'datetime.datetime'對象是預期的。 –

+0

你建議我做什麼?因爲這個錯誤發生在默認的Django管理模型中。我應該嘗試使用另一個JDBC,重寫auth.User模型還是嘗試其他方法? – DMunoz

+1

它聽起來像數據訪問層中的某種錯誤,可能與SQLite實際上沒有本地日期時間存儲類型的方式有關。 – bobince

回答

2

我認爲這是可以很容易解釋:

  1. sqlite3的lacks explicit datetime支持,「bobince」在comments above
  2. django.db.backends.sqlite3通過存儲在ISO-文本圍繞這個作品指出8601格式
  3. 但這不是JDBC支持的一部分,它預計實際的datetime支持

注意有一個outstanding bug補充完滿成功e對Jython的sqlite3支持。我只是將該錯誤的里程碑調整爲2.7.2(可能稍後)。這種支持是我們一段時間想要做的事,儘管它有點複雜,因爲它不僅僅是使用JDBC的問題。

就目前而言,這可能意味着您要麼做自己的解決方法(不知道範圍);或切換到像MySQL或Apache Derby這樣的數據庫(像sqlite3這樣的小腳本,但支持日期時間)。