In my GeoDjango project I want to connect to a legacy PostgreSQL/PostGIS database. It contains the following schemas:
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.
I want the "data" models to connect to the data
schema. I already tried to generate models from the legacy tables but python manage.py inspectdb
connects to the public
schema.
In order to provide access to the different schemas I adapted the approach 2 of this article which preassigns individual search_path
values to specific database users:
-- 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's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.
Rather than re-inventing the wheel, the Django Project has elected to use the psycopg2 library—arguably the most popular python adapter for Postgres.
You have to leverage the 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',
},
}
If you don't need to manage the tables through migrations, you could use escaped quotes for the db_table
attribute of your model:
class SomeModel(models.Model):
field1 = models.AutoField(primary_key=True)
class Meta():
managed=False
db_table=u'"schema\".\"table"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With