我的环境是Python3.5
,Django1.8.3
和cx_Oracle5.3
(由pip3 freeze
检查)。
当调用Django
方法时,Type Error
查询集引发了count()
异常。
当谈到Python2
+ cx_oracle
或Python3
+ sqlite3
工作正常,没有任何例外,但Python3
+ cx_oracle
。
Thue,我试图将cx_Oracle
版本更新为6.1
(最新版本)因为我认为我可能是cx_Oracle
和Python3
之间的兼容性问题。但是,它会生成不同的错误。
我详细介绍了下面的代码块,请参考它。
P.S:我需要将Django
版本保留到1.8.3
以与我的应用程序兼容。
from django.db import models
class Device(models.Model):
deviceClass = models.CharField(max_length=10)
class Meta:
db_table = 'TST_G2S_DEVICE'
$ python3 manage.py shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from polls.models import Device;
In [2]: dev = Device.objects.all()
In [3]: dev
Out[3]: []
In [4]: type(dev)
Out[4]: django.db.models.query.QuerySet
In [5]: dev.count()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-72a7bdf9f7f7> in <module>()
----> 1 dev.count()
/usr/local/lib/python3.5/dist-packages/django/db/models/query.py in count(self)
316 return len(self._result_cache)
317
--> 318 return self.query.get_count(using=self.db)
319
320 def get(self, *args, **kwargs):
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py in get_count(self, using)
462 obj = self.clone()
463 obj.add_annotation(Count('*'), alias='__count', is_summary=True)
--> 464 number = obj.get_aggregation(using, ['__count'])['__count']
465 if number is None:
466 number = 0
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py in get_aggregation(self, using, added_aggregate_names)
443 outer_query.select_related = False
444 compiler = outer_query.get_compiler(using)
--> 445 result = compiler.execute_sql(SINGLE)
446 if result is None:
447 result = [None for q in outer_query.annotation_select.items()]
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type)
838 if result_type == SINGLE:
839 try:
--> 840 val = cursor.fetchone()
841 if val:
842 return val[0:self.col_count]
/usr/local/lib/python3.5/dist-packages/django/db/utils.py in inner(*args, **kwargs)
102 def inner(*args, **kwargs):
103 with self:
--> 104 return func(*args, **kwargs)
105 return inner
106
/usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py in fetchone(self)
507 if row is None:
508 return row
--> 509 return _rowfactory(row, self.cursor)
510
511 def fetchmany(self, size=None):
/usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py in _rowfactory(row, cursor)
575 # This comes from FloatField columns.
576 value = float(value)
--> 577 elif precision > 0:
578 # NUMBER(p,s) column: decimal-precision fixed point.
579 # This comes from IntField and DecimalField columns.
TypeError: unorderable types: NoneType() > int()
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from polls.models import Device;
In [2]: dev = Device.objects.all()
In [3]: dev
Out[3]: <repr(<django.db.models.query.QuerySet at 0x7f0ab12e8fd0>) failed: AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'>
In [4]: type(dev)
Out[4]: django.db.models.query.QuerySet
In [5]: dev.count()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-72a7bdf9f7f7> in <module>()
----> 1 dev.count()
/usr/local/lib/python3.5/dist-packages/django/db/models/query.py in count(self)
316 return len(self._result_cache)
317
--> 318 return self.query.get_count(using=self.db)
319
320 def get(self, *args, **kwargs):
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py in get_count(self, using)
462 obj = self.clone()
463 obj.add_annotation(Count('*'), alias='__count', is_summary=True)
--> 464 number = obj.get_aggregation(using, ['__count'])['__count']
465 if number is None:
466 number = 0
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py in get_aggregation(self, using, added_aggregate_names)
443 outer_query.select_related = False
444 compiler = outer_query.get_compiler(using)
--> 445 result = compiler.execute_sql(SINGLE)
446 if result is None:
447 result = [None for q in outer_query.annotation_select.items()]
/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type)
825 return
826
--> 827 cursor = self.connection.cursor()
828 try:
829 cursor.execute(sql, params)
/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py in cursor(self)
160 self.validate_thread_sharing()
161 if self.queries_logged:
--> 162 cursor = self.make_debug_cursor(self._cursor())
163 else:
164 cursor = self.make_cursor(self._cursor())
/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py in _cursor(self)
135 self.ensure_connection()
136 with self.wrap_database_errors:
--> 137 return self.create_cursor()
138
139 def _commit(self):
/usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py in create_cursor(self)
260
261 def create_cursor(self):
--> 262 return FormatStylePlaceholderCursor(self.connection)
263
264 def _commit(self):
/usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py in __init__(self, connection)
417 self.cursor = connection.cursor()
418 # Necessary to retrieve decimal values without rounding error.
--> 419 self.cursor.numbersAsStrings = True
420 # Default arraysize of 1 is highly sub-optimal.
421 self.cursor.arraysize = 100
AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'
不幸的是,你对django 1.8的限制并不能解决这个问题。
This github问题描述了您遇到的相同情况,具有非常相似的堆栈跟踪。一个修复已经是implemented in Django,但它显然只影响1.11和前向版本,没有backport到1.8的原因
This patch isn't completely compatible with cx_Oracle < 5.2, hence it
won't be backport to Django < 1.11