【问题标题】:Django Testing - I only want one of my databases created - how to specifyDjango 测试 - 我只想创建一个数据库 - 如何指定
【发布时间】:2023-03-31 17:12:01
【问题描述】:

所以在我的 settings.py 中我指定了两个数据库连接(见下文)。

但是当我运行测试时,我只想让 Django 创建“默认”数据库。

有没有办法做到这一点?我尝试添加 TEST_CREATE: False 选项,但我猜出于某种原因这仅适用于 Oracle?

我的 settings.py sn-p:

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
    }

}

【问题讨论】:

  • 也许在设置中查看 sys.argv[1] 以查看您是否处于测试模式,并相应地设置数据库。

标签: django django-testing django-settings


【解决方案1】:

嗯,这是一种方法。在设置了我的普通数据库后,我将它添加到了 settings.py 中。

if 'test' in sys.argv:
    DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3',}}

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2015-12-15
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多