首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django queryset count()方法引发"TypeError: unorderable类型: NoneType() > int()“

Django queryset count()方法引发"TypeError: unorderable类型: NoneType() > int()“
EN

Stack Overflow用户
提问于 2017-12-23 14:19:17
回答 1查看 466关注 0票数 0

我的环境是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

代码语言:javascript
复制
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

代码语言:javascript
复制
$ 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

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-23 14:31:52

不幸的是,您在django 1.8上停留的限制不会解决这个问题。

这个github问题描述了与您所遇到的情况相同的情况,其堆栈跟踪非常相似。修复程序是在Django实施,但是它显然只影响1.11和转发版本,由于这个原因,没有支持到1.8

代码语言:javascript
复制
This patch isn't completely compatible with cx_Oracle < 5.2, hence it 
won't be backport to Django < 1.11
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47953230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档