从Django settings.py导入数据库?

时间:2015-02-05 19:46:22

标签: python django mongodb heroku

我正在编写一个使用MongoDB作为主数据库的Django应用程序。我只需要应用程序对数据库进行查询(在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

def index(request):
    context = RequestContext(request)

    # !!!!!! I want to do something like this:
    rooms = db.studybug.find()

    return render_to_response('studybug/index.html', rooms, context)

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

我真的没有看到为此定义模型的重点或需要,因为操作非常轻巧。

有没有办法做类似的事情:

from settings import db

...

谢谢!

0 个答案:

没有答案
相关问题