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

When I run

python manage.py inspectdb --database=sybase_database

it ends with error message:

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 only supports up to Django 1.8.5 (Django 1.7.0 for inspectdb command).
It is no longer maintained; it was last updated in May 2016.

You can patch for later versions of Django in manage.py or an AppConfig.

# 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

References:

  • https://github.com/django/django/pull/2154 regarding CursorWrapper
  • https://github.com/django/django/pull/7206 regarding DatabaseWrapper
  • https://github.com/django/django/pull/8889 regarding typecast_decimal
  • https://github.com/sqlanywhere/sqlany-django/pull/15 regarding typecast_decimal
  • https://sqlanywhere-forum.sap.com/questions/37563/sqlany-django-driver-upgrade-to-django-32 requesting an update to sqlany_django (unanswered as of this post)

Stack traces:

  1. Regarding 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. Regarding 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__

Replaced typecast_decimal() with decimal.Decimal()

change base.py code like to

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

and in utils.py you can return

decimal.Decimal()

maybe help to you