Django oracle数据库设置

时间:2017-12-28 17:23:08

标签: django oracle django-models oracle11g django-oracle

我只想将我的本地oracle db与我的django项目连接,但我的数据库凭据无法正常工作。实际上,我能够通过sql developer将我的oracle数据库与该凭证连接: enter image description here

我刚才在django settings_py中使用了该凭据

 function least(num){
  var res = "";
  for(var char of num.toString().split("").reverse()){
    if(+res){
      res = (char === "." ? "." : "0") + res;
    } else {
      res = char + res;
    }
  }
  return res;
}

,错误是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'INTERNAL',
        'USER': 'system',
        'PASSWORD': 'oracle',        
        'HOST':'localhost/xe',
        'PORT':'1521'
    }
}

这是我的听众状态

Traceback (most recent call last):
web_1  |   File "manage.py", line 22, in <module>
web_1  |     execute_from_command_line(sys.argv)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
web_1  |     utility.execute()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
web_1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
web_1  |     self.execute(*args, **cmd_options)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
web_1  |     output = self.handle(*args, **options)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 110, in handle
web_1  |     loader.check_consistent_history(connection)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 282, in check_consistent_history
web_1  |     applied = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
web_1  |     self.ensure_schema()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
web_1  |     if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
web_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
web_1  |     raise value.with_traceback(tb)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/oracle/base.py", line 212, in get_new_connection
web_1  |     return Database.connect(self._connect_string(), **conn_params)
web_1  | django.db.utils.DatabaseError: ORA-12545: Connect failed because target host or object does not exist

1 个答案:

答案 0 :(得分:0)

您应该将HOST更改为localhost' or '127.0.0.1,并将SID更改为NAME

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'xe',
        'USER': 'system',
        'PASSWORD': 'oracle',        
        'HOST':'127.0.0.1',
        'PORT':'1521'
    }
}

对于将来的引用,如果Oracle配置了Service name而不是SID,那么配置将是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': '127.0.0.1:1521/service.name',
        'USER': 'system',
        'PASSWORD': 'oracle',        
    }
}

在Django中使用Oracle时要考虑的另一件事是,当您连接到其他用户(架构)数据库时,您必须在Django模型中设置db_table Meta option

class OracleTable(models.Model):
    ... fields ...
    class Meta:
        db_table = '\"OTHERUSER\".\"ORACLETABLE\"'