简体   繁体   中英

Problem with running IBM DB2 connection in Django for Windows

I'm new to Django and am trying to connect two databases. One is a.sqlite3 DB and the other is a DB2 database.

`DATABASES = {
    'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        },
    'DB_2': {
      'ENGINE': 'ibm_db_django',
      'NAME': 'DB_Name',
      'USER': 'my_username',
      'PASSWORD': 'my_pass',
      'HOST': 'external_host',
      'PORT': '50001',
      'PCONNECT': True,
      },
}`

My sqlite3 DB works fine, but when I try to run python manage.py test or python manage.py migrate I get the following error:

django.core.exceptions.ImproperlyConfigured: 'ibm_db_django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

In looking at the IBM website, it looks like I need to install ibm_db_django, which I've done. I feel like it has to do with how my engine is defined, like I need it to point somewhere else on the computer. If that is that case, I've no clue where it's supposed to be. Could someone please help point me in the right direction?

python -m django --version
3.0.3

python -V
Python 3.7.7

I'm using 64-bit python

When I drop the ibm_db_django folder into the django/db/backends directory and point my DB_2 connection to it, I get the following errors:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\test\runner.py", line 684, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\test\runner.py", line 606, in setup_databases
    self.parallel, **kwargs
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\test\utils.py", line 156, in setup_databases
    test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\test\utils.py", line 260, in get_unique_databases_and_mirrors
    connection = connections[alias]
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\db\utils.py", line 207, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\db\utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\db\backends\ibm_db_django\base.py", line 53, in <module>
    import ibm_db_django.pybase as Base
  File "C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\ibm_db_django\pybase.py", line 38, in <module>
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (C:\Users\etaurone\Anaconda3\envs\RTP_WebPage\lib\site-packages\django\utils\__init__.py)

Thank you very much.

Currently (May 2020) the python module ibm_db_django may exhibit issues with Django versions above 2.0.0. Some of the issues have workarounds, others do not.

At the present date, the ibm_db_django readme page on github states: "Django Framework Minimum 1.7.x and Maximum 2.0"

You write that your Django version is v3 which may not yet be fully supported by ibm_db_django , although workarounds are possible in many cases, even to allow the initial manage.py migrate to succeed with Db2-LUW at least. But other issues exist, so it may be wise to stay with the supported (tested) Django max release 2.0.0.

This situation may change when developers release newer versions of ibm_db_django . Please refer to the github issues page.

If you change your Django version, it is wise to make a new project ( django-admin startproject ), and merge your changes into its settings.py file.

Otherwise the previous settings.py may cause the new Django version to throw exceptions.

I choose to use a virtualenv in Python (via virtualenvwrapper-win ) but although this is recommended, it is not essential.

On Microsoft-Windows10 x64 1909, the following combination results in successful manage.py migrate (although the metadata will be in the default database, which may not be what you want):

  • python 3.7.7 (64-bit)

  • pip 20.1.1

  • Django 2.0.0 ( <= 2.2.12 works for initial migration).

  • ibm-db 3.0.1

  • ibm-db-django 1.2.0.0a0

To install a specific version xyz of a moduleName, first uninstall any existing version, then use the python -m pip install moduleName==xyz

For my settings.py I apply these changes:

#USE_TZ = True

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.flatpages',
    'django.contrib.redirects',
    'django.contrib.sites',
]


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'sample' : {
        'ENGINE'  : 'ibm_db_django',
        'NAME'    : 'sample',
        'USER'    : 'xxxxx',
        'PASSWORD': 'xxxxxxxx',
        'HOST'    : 'xxxxxxx',
        'PORT'    : '50000',
    }
}

The above combination allows the python manage.py migrate to complete without errors with a local or remote Db2-LUW database, as long as the databases are correctly catalogued.

If you want the django metadata (8 tables) to be in a Db2 database (instead of the database marked as the default in settings.py) then you must either alter the default in settings.py or use the -- database option to manage.py migrate .

When you choose the Db2 database to store the metadata, the manage.py migrate may fail (at the very end) with a known issue , although the migration has in fact created all relevant migrations, the error happens during cleanup. The workaround shown on the github issue is also effective for Db2 with Django 2.2.x releases at least up to 2.2.12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM