如何在django中运行"syncdb"时指定模式名称?

Dev*_*eph 7 python database django

假设我有一个名为"my_schema"的模式,如何为该特定模式创建带有"django syncdb"的表?或者还有其他选择从我的django模型快速创建表格?我认为,默认情况下,django会为"公共"模式创建表.

rom*_*rcz 6

首先,你必须有psycopg2> = 2.4.3 [1].如果您有,那么您可以在基于字典的数据库配置中向OPTIONS添加模式,如下所示:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    # ...
    'OPTIONS': {
      'options': '-c search_path=my_schema'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在postgresql 8.4和django 1.3上测试过

  1. http://initd.org/psycopg/docs/module.html?highlight=connect#psycopg2.connect
  2. http://www.postgresql.org/docs/8.4/static/libpq-connect.html#LIBPQ-CONNECT-OPTIONS