Django配置不同的数据库

时间:2017-11-30 17:49:07

标签: django django-settings django-database django-mysql

我感兴趣的是2 database,基本的和发展的:{/ p>

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

在我的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',
    }
}

允许的主机是:

ALLOWED_HOSTS = ['xxx.xx.xxx.xx']  # I replaced it for the example

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

$ python ./manage.py runserver xxx.xx.xxx.xx:8006

在这里我修改了生产数据库。我可以切换到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',
    }
}

它工作正常,服务器正与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',
    }   
}

现在当我在xxx.xx.xxx.xx:8006上打开网页时,我收到此错误:

ImproperlyConfigured at /admin/

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

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

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)

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

2 个答案:

答案 0 :(得分:2)

我想说,为devprod创建单独的设置文件。或者仅针对数据库的情况,您可以使用环境变量。

$ export ENV=PROD

然后在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',
        },
    } 

答案 1 :(得分:1)

如果您未指定默认数据库,则需要使用自动数据库路由器来指定模型将使用的数据库。您可以编写这些路由器类,然后指定DATABASE_ROUTERS设置以指向它们。路由器基本上包含一些逻辑,用于告知数据库操作的去向,并且可以检查这是否是一个开发环境。

相关问题