簡體   English   中英

如何使django應用在開發服務器中使用sqlite而不在生產服務器上使用sqlite

[英]How can I make my django app use sqlite in the development server but not on to production server

我在生產服務器上使用PostresSQL,但這並不是問題所在。

我聽說有一種簡單的設置方法,即使我的項目是從同一存儲庫中提取的,我也可以將其設置為針對所在環境使用正確的數據庫。

最簡單的方法是:

if DEBUG:
    # My debug config
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            ...
        }
    }
else:
    # My production config
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            ...
        }
    }

在我工作的大多數項目中,我們都使用更清潔的方法。 我們沒有setup.py ,而是setup是一個包含多個模塊的軟件包。 看起來像:

# proj/app/settings/__init__.py

from .settings.common import *  # proj/app/settings/common.py
from .settings.something_else import *

try:
    from .settings.development import *
    # if successful, we're in the development environment
    # inside of the development.py you can redefine everything
    # includig DATABASES
except ImportError:
    # don't have settings/development.py
    assert DEBUG is False
    # we're on production

然后project/app/settings/development.py僅出現在開發機器上,並且包含所有與開發相關的配置。

除非您要在生產中使用DEBUG,否則此解決方案實際上不僅可以為您提供正確的DATABASES設置,而且還可以保護您免於意外將啟用DEBUG的項目投入生產中:

if DEBUG:
    DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
else:
    DATABASES = {
        *your production DB settings*
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM