【问题标题】:Django queryset count() method raise "TypeError: unorderable types: NoneType() > int()"Django queryset count() 方法引发“TypeError:不可排序的类型:NoneType() > int()”
【发布时间】:2018-06-05 19:11:21
【问题描述】:

我的环境是Python3.5Django1.8.3cx_Oracle5.3(它们是由pip3 freeze检查的)。

Django 查询集在调用count() 方法时引发Type Error 异常。

当涉及到Python2 + cx_oraclePython3 + sqlite3 工作正常,但Python3 + cx_oracle 没有任何例外。

周二,我尝试将cx_Oracle 版本更新为6.1(最新版本),因为我认为cx_OraclePython3 之间可能存在兼容性问题。但是,它会产生不同的错误。

我用下面的代码块详细说明,请参考。

P.S:我需要将 Django 版本保持为 1.8.3 以与我的应用程序兼容。

models.py

​​>
from django.db import models

class Device(models.Model):
    deviceClass     = models.CharField(max_length=10)

    class Meta:
        db_table = 'TST_G2S_DEVICE'

cx_Oracle5.3

$ 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()

cx_Oracle6.1

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 oracle python-3.x cx-oracle


    【解决方案1】:

    很遗憾,您对 django 1.8 的限制无法解决此问题。

    This github 问题描述了您遇到的相同情况,具有非常相似的堆栈跟踪。修复了 implemented in Django,但它显然只影响 1.11 和正向版本,因此没有向后移植到 1.8

    This patch isn't completely compatible with cx_Oracle < 5.2, hence it 
    won't be backport to Django < 1.11
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多