模块 'django.db.backends.utils' 没有属性 'typecast_decimal'

module 'django.db.backends.utils' has no attribute 'typecast_decimal'

当我运行

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

已将 typecast_decimal() 替换为 decimal.Decimal()

将 base.py 代码更改为

Database.register_converter("decimal", decoder(decimal.Decimal))

在utils.py中你可以return

decimal.Decimal()

也许对你有帮助

sqlany-django 最多只支持 Django 1.8.5(Django 1.7.0 用于 inspectdb 命令)。
它不再维护;最后更新于 2016 年 5 月。

您可以在 manage.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

参考文献:


堆栈跟踪:

  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__