如何从 Django 连接到多个 PostgreSQL 模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31726064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to multiple PostgreSQL schemas from Django?
提问by JJD
In my GeoDjango project I want to connect to a legacy PostgreSQL/PostGIS database. It contains the following schemas:
在我的 GeoDjango 项目中,我想连接到旧的 PostgreSQL/PostGIS 数据库。它包含以下模式:
- data // contains all the geospatial data
- django // empty, created by me
- public // system tables such as
spatial_ref_sys
- data // 包含所有地理空间数据
- django // 空,由我创建
- public // 系统表,例如
spatial_ref_sys
I want the Django tables shown in the screenshot to go into the django
schema. I do not want to pollute the public
schema.
我希望屏幕截图中显示的 Django 表进入django
架构。我不想污染public
模式。
I want the "data" models to connect to the data
schema. I already tried to generate models from the legacy tablesbut python manage.py inspectdb
connects to the public
schema.
我希望“数据”模型连接到data
模式。我已经尝试从旧表生成模型但python manage.py inspectdb
连接到public
模式。
In order to provide access to the different schemas I adapted the approach 2 of this articlewhich preassigns individual search_path
values to specific database users:
为了提供对不同模式的访问,我调整了本文的方法 2,该方法将各个search_path
值预先分配给特定的数据库用户:
-- user accessing django schema...
CREATE ROLE django_user LOGIN PASSWORD 'secret';
ALTER ROLE django_user SET search_path TO django, public;
-- user accessing data schema...
CREATE ROLE data_user LOGIN PASSWORD 'secret';
ALTER ROLE data_user SET search_path TO data, public;
Then I configured the database connections as follows:
然后我配置了数据库连接如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'multi_schema_db',
'USER': 'django_user',
'PASSWORD': 'secret',
},
'data': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'multi_schema_db',
'USER': 'data_user',
'PASSWORD': 'secret',
},
}
How can I actually configure that Django uses the django
schema while "data" models connect to the data
schema?
django
当“数据”模型连接到模式时,我如何实际配置 Django 使用data
模式?
Readings
读物
回答by JJD
You have to leverage the search_path
:
您必须利用search_path
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS' : {
'options': '-c search_path=django,public'
},
'NAME': 'multi_schema_db',
'USER': 'django_user',
'PASSWORD': 'secret',
},
'data': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS' : {
'options': '-c search_path=data,public'
},
'NAME': 'multi_schema_db',
'USER': 'data_user',
'PASSWORD': 'secret',
},
}
回答by wbloos
If you don't need to manage the tables through migrations, you could use escaped quotesfor the db_table
attribute of your model:
如果您不需要通过迁移来管理表,则可以对模型的属性使用转义引号db_table
:
class SomeModel(models.Model):
field1 = models.AutoField(primary_key=True)
class Meta():
managed=False
db_table=u'"schema\".\"table"'
回答by Incognos
We use Django Tenant Schemaswith great success. It allows you to access different schemas by delineating different tenants as the owners of the schemas.
我们使用Django 租户架构取得了巨大成功。它允许您通过将不同的租户描述为架构的所有者来访问不同的架构。
This will allow you to set the schema on a per call basis. If the schema needs to be set on a per url basis, you can do that in middleware.
这将允许您在每次调用的基础上设置架构。如果需要基于每个 url 设置架构,您可以在中间件中执行此操作。