模块'django.db.ackends.utils'没有属性'type_decimal'

2022-04-27 00:00:00 django sybase sqlanydb

当我运行时

python manage.py inspectdb --database=sybase_database

以错误消息结束:

Database.register_converter(Database.DT_DECIMAL, util.typecast_decimal) AttributeError: module 'django.db.backends.utils' has no attribute 'typecast_decimal'

$ pip freeze
certifi==2021.10.8
chardet==3.0.4
defusedxml==0.7.1
Django==2.2.4
django-allauth==0.40.0
django-bootstrap-form==3.4
django-bootstrap3==15.0.0
django-crispy-forms==1.7.2
django-crudbuilder==0.2.7
django-debug-toolbar==1.10.1
django-filter==2.2.0
django-mssql-backend==2.8.1
django-tables2==2.4.1
idna==2.8
importlib-metadata==2.1.1
oauthlib==3.1.1
pyodbc==4.0.32
python-dateutil==2.8.2
python3-openid==3.2.0
pytz==2021.3
requests==2.21.0
requests-oauthlib==1.3.0
six==1.16.0
sqlany-django==1.13
sqlanydb==1.0.11
sqlparse==0.4.2
urllib3==1.24.3
zipp==3.6.0

Ubuntu 18.04


解决方案

sqlany-django最高仅支持Django 1.8.5(inspectdb命令的Django 1.7.0)。
不再维护;上次更新是在2016年5月。

您可以在made.py或AppConfig中修补Django的更高版本。

# Django >= 2.0, for sqlany_django/base.py
import decimal
from django.db.backends import utils
utils.typecast_decimal = decimal.Decimal

# Django >= 1.11, for BaseDatabaseWrapper
from sqlany_django.base import DatabaseWrapper
DatabaseWrapper.client_class = lambda self, connection: None
DatabaseWrapper.creation_class = lambda self, connection: None
DatabaseWrapper.features_class = lambda self, connection: None
DatabaseWrapper.introspection_class = lambda self, connection: None
DatabaseWrapper.ops_class = lambda self, connection: None

# Django >= 1.7.1, for inspectdb
from django.db.backends import utils
from sqlany_django import base
class CursorWrapper(base.CursorWrapper, utils.CursorWrapper):
    pass
base.CursorWrapper = CursorWrapper

引用:

  • https://github.com/django/django/pull/2154关于CursorWrapper
  • https://github.com/django/django/pull/7206关于DatabaseWrapper
  • https://github.com/django/django/pull/8889关于typecast_decimal
  • https://github.com/sqlanywhere/sqlany-django/pull/15关于typecast_decimal
  • https://sqlanywhere-forum.sap.com/questions/37563/sqlany-django-driver-upgrade-to-django-32请求更新sqlany_django(截至本帖子未回复)

堆栈跟踪:

  1. 关于DatabaseWrapper
Traceback (most recent call last):
  ...
  File "/path/to/site-packages/django/core/management/commands/inspectdb.py", line 40, in handle_inspection
    connection = connections[options['database']]
  File "/path/to/site-packages/django/db/utils.py", line 207, in __getitem__
    conn = backend.DatabaseWrapper(db, alias)
  File "/path/to/site-packages/sqlany_django/base.py", line 447, in __init__
    super(DatabaseWrapper, self).__init__(*args, **kwargs)
  File "/path/to/site-packages/django/db/backends/base/base.py", line 102, in __init__
    self.client = self.client_class(self)
TypeError: 'NoneType' object is not callable
  1. 关于CursorWrapper
Traceback (most recent call last):
  ...
  File "/path/to/site-packages/django/core/management/commands/inspectdb.py", line 47, in handle_inspection
    with connection.cursor() as cursor:
AttributeError: __enter__

相关文章