2014-02-24 64 views

回答

3

基於this Gist,我修改django.core.management.commands.inspectdb:圍繞線32,在handle_inspection()cursor = connection.cursor()後,添加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") 
    # [...] 

截至,至少,Django的1.9:

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

[這是現在的'Django的extensions'部分(https://github.com/abourguignon/django-extensions/commit/573ae547862b173d00011e892c974b38b30dbc26) – Anto

+0

凡在Django的擴展? – TimRich

+0

[There](https://github.com/django-extensions/django-extensions/pull/485)。 – Anto

4

是的,你必須指定SEARCH_PATH,通過在settings.py在你的數據庫變量增加一個選項,這樣的:

'OPTIONS': { 
     'options': '-c search_path=myschema' 
} 

的整個數據庫時變量應該是:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', 
     'NAME': 'mydatabase', 
     'USER': 'postgres', 
     'PASSWORD': 'mypassword', 
     'HOST': 'localhost', 
     'PORT': '5432', 
     'OPTIONS': { 
      'options': '-c search_path=myschema' 
     } 
    } 
} 

python manage.py inspectdb後應該在你的架構工作

+1

這個工程,但命令是'python manage.py inspectdb' –

+0

謝謝!我已經編輯了答案 – ugosan