【问题标题】:How to set up multiple schema in django with oracle as a database?如何使用 oracle 作为数据库在 django 中设置多个模式?
【发布时间】:2019-11-21 18:04:26
【问题描述】:

我的工作场所计划使用 Python/Django 作为后端框架,并在前端使用 React,在 ASP 经典的 oracle db 之上。由于我们的 oracle 数据库从公司成立之初就开始构建,因此我们决定保持原样。

据我了解,oracle 中的模式通常通过用户名/密码访问,因此每个模式都需要用户/密码才能访问,而我们的 oracle 数据库有大约 30 个模式,每个模式都包含大量表。

另一方面,Django 似乎一次只支持一个模式,基于安装在 settings.py 中的应用程序,这听起来像数据库配置需要在 settings.py 中设置不同的用户/密码为每个安装的应用程序。

到目前为止,我已经在settings.py 中尝试过class MetaDATABASES

    // class Meta
    class SomeModel(models.Model):
            ///some fields and data types...

            class Meta():
                    managed=False
                    db_table=u'"schema\".\"table"'

    // DATABASES
    DATABASES = {
          'default': {
                'ENGINE': 'django.db.backends.oracle',
                'NAME': 'multi_schema_db',
                'USER': 'django_user',
                'PASSWORD': 'secret',
          },

          'data': {
                'ENGINE': 'django.db.backends.oracle',
                'NAME': 'multi_schema_db',
                'USER': 'data_user',
                'PASSWORD': 'secret',
          },
    }

我的问题是,是否有任何解决方法可以访问 django 仅安装一个应用的多个架构?

附:纠正我上面的任何误解。

【问题讨论】:

    标签: python django database oracle schema


    【解决方案1】:

    You can have multiple schemas in Django

    DATABASES = {
          'default': {
                'ENGINE': 'django.db.backends.oracle',
                'NAME': 'multi_schema_db_1',  # The name is the schema
                'USER': 'django_user',
                'PASSWORD': 'secret',
          },
    
          'data': {
                'ENGINE': 'django.db.backends.oracle',
                'NAME': 'multi_schema_db_2',  # The name is the schema
                'USER': 'data_user',
                'PASSWORD': 'secret',
          },
    }
    

    要使用特定架构,请使用 .using()

    SomeModel.objects.using('data').all()  # The default is to use the "default" database conection
    

    如果某些模型只在一个模式中,您可以使用 routers 定义每个模型使用哪个数据库

    class YourRouter:
    
        def db_for_read(self, model, **hints):
            return database_for_the_model
    
        def db_for_write(self, model, **hints):
            return database_for_the_model
    

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2018-06-07
      • 1970-01-01
      • 2019-02-05
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多