Run a test on a specific pattern

I would like to use inspectdb to create appropriate models for the tables just presented. But it looks like this command is only looking for a schema public, and new tables in another.

Is it possible to point the circuit to inspectdb?

+3
source share
2 answers

Based on this Gist , I changed 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")
    # [...]

At a minimum, Django 1.9:

def handle_inspection(self, options):
    # [...]
    with connection.cursor() as cursor:
        cursor.execute("SET search_path TO myotherschema")
        # [...]
+3
source

Yes, you need to specify the search_ path by adding a parameter to your DATABASES variable in settings.py, for example:

'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 inspectdbshould work on your circuit

+5
source

All Articles