I'd like to use inspectdb in order to build corresponding models for freshly introduced tables. But is looks like this command only looks up the public
schema, while the new tables are in another one.
Is it possible to specify a schema to inspectdb
?
Yes, you have to specify the search_path, by adding an option in your DATABASES variable at settings.py, like that:
'OPTIONS': {
'options': '-c search_path=myschema'
}
The full DATABASES variable should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'postgres',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'options': '-c search_path=myschema'
}
}
}
After that python manage.py inspectdb
should work on your schema
Based on this Gist, I modified django.core.management.commands.inspectdb
: around line 32, in handle_inspection()
, after cursor = connection.cursor()
, add cursor.execute("SET search_path TO myotherschema")
.
def handle_inspection(self, options):
connection = connections[options.get('database')]
table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
cursor = connection.cursor()
cursor.execute("SET search_path TO myotherschema")
# [...]
As of, at least, Django 1.9:
def handle_inspection(self, options):
# [...]
with connection.cursor() as cursor:
cursor.execute("SET search_path TO myotherschema")
# [...]
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