所以在我的 settings.py 中我指定了两个数据库连接(见下文)。
但是当我运行测试时,我只希望 Django 创建“默认”数据库。
有什么办法吗?我尝试添加 TEST_CREATE: False 选项,但我猜出于某种原因这仅适用于 Oracle?
我的 settings.py 片段:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'App1', # Or path to database file if using sqlite3.
'USER': 'webaccess', # Not used with sqlite3.
'PASSWORD': '***', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
},
'default_root': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'App1', # Or path to database file if using sqlite3.
'USER': 'django', # Not used with sqlite3.
'PASSWORD': '****', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'TEST_CREATE': False #Don't create when testing
}
}
最佳答案
好吧,这是一种方法。我在设置我的普通数据库后将它添加到 settings.py 中。
if 'test' in sys.argv:
DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3',}}
关于Django 测试 - 我只想创建我的数据库之一 - 如何指定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553670/