从Django导入数据库设置.py?

2024-04-25 15:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Django应用程序,它使用MongoDB作为它的主数据库。我只需要应用程序对数据库(托管在Heroku上)进行查询,并显示每个用户请求的结果。在

我知道PyMongo等Python模块的存在可以方便地与MongoDB连接/交互,但我不想每次用户请求页面时都建立数据库连接。我希望数据库在启动Django应用程序时连接。在

现在在我的settings.py文件中,我有:

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'heroku_app33177236',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        # 'USER': 'admin',
        # 'PASSWORD': '',
        'HOST': 'mongodb://admin:#####@ds041581.mongolab.com:41581/heroku_app33177236',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        #'PORT': '',                      # Set to empty string for default.
    }
}

在我的views.py中:

^{pr2}$

正如您在上面看到的,我只想在每次用户请求时查询数据库并显示结果。在

我真的不明白为此定义模型的意义或必要性,因为操作非常轻量级。在

有没有办法做到:

from settings import db

??在

谢谢!在


Tags: ortodjango用户py数据库应用程序default

热门问题