Django 配置不同的数据库

kal*_*gne 1 django django-database django-settings django-mysql

我有两个database兴趣,基本的和开发的:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| projectsdb         |
| projectsdb_dev     |
+--------------------+
3 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

在我的 django 文件中mysite/mysite/settings.py,我的数据库是这样声明的:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}
Run Code Online (Sandbox Code Playgroud)

允许的主机是:

ALLOWED_HOSTS = ['xxx.xx.xxx.xx']  # I replaced it for the example
Run Code Online (Sandbox Code Playgroud)

我在用于开发的端口 8006 上启动服务器:

$ python ./manage.py runserver xxx.xx.xxx.xx:8006
Run Code Online (Sandbox Code Playgroud)

这里我修改了生产数据库。我可以切换到 dev 数据库替换默认数据库名称:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,服务器正在与projectsdb_dev数据库交互。不过,我想在设置文件中保留两个数据库可用,并且我看到了这样设置的教程:

DATABASES = { 
    'default': {}, 
    'prod': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    },  
    'dev': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }   
}
Run Code Online (Sandbox Code Playgroud)

现在,当我打开 xxx.xx.xxx.xx:8006 上的网页时,出现以下错误:

ImproperlyConfigured at /admin/

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
Run Code Online (Sandbox Code Playgroud)

我不知道它是否相关,但我也有这张表:

mysql> select * from django_site;
+----+--------------------+----------------+
| id | domain             | name           |
+----+--------------------+----------------+
|  1 | example.com        | example.com    |
|  2 | xxx.xx.xxx.xx:8000 | projectsdb     |
|  3 | xxx.xx.xxx.xx:8006 | projectsdb_dev |
+----+--------------------+----------------+
3 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

如何运行指定我想要的正确数据库的服务器?

itz*_*nTV 6

我想说,为dev和创建单独的设置文件prod。或者对于数据库的情况,只能使用环境变量。

$ export ENV=PROD
Run Code Online (Sandbox Code Playgroud)

然后在settings.py

import os
if os.environ.get('ENV') == "PROD":
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        }, 
    }
else:
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        },
    } 
Run Code Online (Sandbox Code Playgroud)