Db2 driver for Django?

流过昼夜 提交于 2020-07-23 06:40:16

问题


It seems to me that Django currenty only provides 4 backend database engine which are:

  1. 'django.db.backends.postgresql'
  2. 'django.db.backends.mysql'
  3. 'django.db.backends.sqlite3'
  4. 'django.db.backends.oracle'

If I use MySQL, all I have to do is just fill the Engine with 'django.db.backends.mysql'. But now, because my main Database is DB2, I'm having some issues to connect it with pyodbc. What is needed?

import os

SECRET_KEY = 'o0zdmzzdw44tkd5a7o*h-@*bo)r@f#)!)$)8f-y%=sn*kr)@t%'

DEBUG = True

ALLOWED_HOSTS = [127.0.0.1]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'arapi.apps.ArapiConfig', #my projeck
    'rest-framework', 
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'arDRFAPIWEB.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'arDRFAPIWEB.wsgi.application'

DATABASES = {
      'default': {
          'ENGINE': 'ibm_db_django',
          'NAME': as400,
          'USER' : root,
          'PASSWORD' : root,
          'HOST' : as400.****.co.id,
          'PORT' : 3306,
          'PCONNECT'   :  True, #Optional property, default is false
      }
   }

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

When I run python manage.py test I got this error

Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'

Although I've downgrade the python into 3.6.5 version and my django is 2.1.1 version which I thought this might solve the problem.


回答1:


There is a Django driver available for IBM Db2. It is even referenced in the Db2 documentation and there is a document which describes the setup procedure for Db2 and Django. There, they suggest a configuration like this:

DATABASES = {
  'default': {
  'ENGINE'   : 'ibm_db_django',
  'NAME'     : 'database',
  'USER'     : 'user',
  'PASSWORD' : 'password',
  'HOST'     : 'localhost',
  'PORT'     : '50000',
  'PCONNECT' :  True,      #Optional property. It is true by default
  }
}

You can find the driver package ibm_db_django in the related GitHub repo.

You need to specify ibm_db_django instead of, e.g., django.db.backends.mysql. django.db.backends.db2 will give an error.



来源:https://stackoverflow.com/questions/61672647/db2-driver-for-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!