Django queryset count()方法raise;TypeError:无序类型:NoneType()>;int();
我的环境是Django queryset count()方法raise;TypeError:无序类型:NoneType()>;int();,django,oracle,python-3.x,cx-oracle,Django,Oracle,Python 3.x,Cx Oracle,我的环境是Python3.5、Django1.8.3和cx\u Oracle5.3(它们由pip3-freeze检查) Django查询集在调用count()方法时引发类型错误异常 当涉及到Python2+cx\u-oracle或Python3+sqlite3时,除了Python3+cx\u-oracle之外,一切正常 因此,我尝试将cx\u-Oracle版本更新为6.1(最新版本),因为我认为cx\u-Oracle和Python3之间可能存在兼容性问题。但是,它会生成一个不同的错误 我用下面的
Python3.5
、Django1.8.3
和cx\u Oracle5.3
(它们由pip3-freeze
检查)
Django
查询集在调用count()
方法时引发类型错误
异常
当涉及到Python2
+cx\u-oracle
或Python3
+sqlite3
时,除了Python3
+cx\u-oracle
之外,一切正常
因此,我尝试将cx\u-Oracle
版本更新为6.1
(最新版本),因为我认为cx\u-Oracle
和Python3
之间可能存在兼容性问题。但是,它会生成一个不同的错误
我用下面的代码块详细说明,请参考
注:为了与我的应用程序兼容,我需要将Django
版本保留为1.8.3
models.py
cx_Oracle5.3
$python3 manage.py shell
Python 3.5.2(默认值,2017年11月23日,16:37:01)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython2.4.1——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
In[1]:从polls.models导入设备;
在[2]中:dev=Device.objects.all()
在[3]中:dev
输出[3]:[]
在[4]中:类型(dev)
Out[4]:django.db.models.query.QuerySet
在[5]:dev.count()中
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1开发计数()
/计数中的usr/local/lib/python3.5/dist-packages/django/db/models/query.py(self)
316返回len(自身结果缓存)
317
-->318返回self.query.get\u计数(使用=self.db)
319
320 def get(自身、*args、**kwargs):
/get_count中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py(self,using)
462 obj=self.clone()
463对象添加注释(计数(“*”),别名=“\u计数”,is\u summary=True)
-->464 number=obj.get_聚合(使用[''计数'])[''计数']
465如果数字为无:
466数字=0
/get_aggregation中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py(self、using、added_aggregate_名称)
443外部查询。选择相关的=False
444编译器=外部查询。获取编译器(使用)
-->445结果=编译器。执行sql(单个)
446如果结果为无:
447 result=[outer_query.annotation_select.items()中的q为无]
/execute_sql(self,result_type)中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py
838如果结果_类型==单个:
839尝试:
-->840 val=cursor.fetchone()
841如果val:
842返回值[0:自身列计数]
/内部的usr/local/lib/python3.5/dist-packages/django/db/utils.py(*args,**kwargs)
102 def内部(*args,**kwargs):
103与自我:
-->104返回函数(*args,**kwargs)
105返回内部
106
/fetchone(self)中的usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py
507如果行为无:
508返回行
-->509返回_rowfactory(行,self.cursor)
510
511 def FETCHTMANY(自身,大小=无):
/usr/local/lib/python3.5/dist-packages/django/db/backends/oracle/base.py in_rowfactory(行,光标)
575#这来自FloatField列。
576值=浮动(值)
-->577 elif精度>0:
578#数字(p,s)列:十进制精度固定点。
579#这来自IntField和DecimalField列。
TypeError:无序类型:NoneType()>int()
cx_Oracle6.1
Python 3.5.2(默认,2017年11月23日,16:37:01)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython2.4.1——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
In[1]:从polls.models导入设备;
在[2]中:dev=Device.objects.all()
在[3]中:dev
出[3]:
在[4]中:类型(dev)
Out[4]:django.db.models.query.QuerySet
在[5]:dev.count()中
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1开发计数()
/计数中的usr/local/lib/python3.5/dist-packages/django/db/models/query.py(self)
316返回len(自身结果缓存)
317
-->318返回self.query.get\u计数(使用=self.db)
319
320 def get(自身、*args、**kwargs):
/get_count中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py(self,using)
462 obj=self.clone()
463对象添加注释(计数(“*”),别名=“\u计数”,is\u summary=True)
-->464 number=obj.get_聚合(使用[''计数'])[''计数']
465如果数字为无:
466数字=0
/get_aggregation中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py(self、using、added_aggregate_名称)
443外部查询。选择相关的=False
444编译器=外部查询。获取编译器(使用)
-->445结果=编译器。执行sql(单个)
446如果结果为无:
447 result=[outer_query.annotation_select.items()中的q为无]
/execute_sql(self,result_type)中的usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py
825返回
826
-->827游标=自身。
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'
This patch isn't completely compatible with cx_Oracle < 5.2, hence it
won't be backport to Django < 1.11