在Django中如何知道数据库的当前名称? [英] How to know current name of the database in Django?

查看:147
本文介绍了在Django中如何知道数据库的当前名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的django项目中编写测试。现在,我有两个数据库连接:

 (settings.py)
DATABASES = {
'默认':{
'ENGINE':'django.db.backends.postgresql_psycopg2',
'NAME':'db_name'
...
},

和与MongoDB的自定义连接:

 导入sys 
从pymongo import连接
从pymongo.errors import ConnectionFailure
try:
connection = Connection(host =localhost port = 27017)
db = connection ['db_name']
printConnected successfully(Mongo,db_name)
除了ConnectionFailure,e:
sys.stderr.write(无法连接到MongoDB:%s%e)
sys.exit(1)

我想知道我的项目是否在通过

  python manage.py test myapp 

因为运行测试时,django会自动创建单独的DB(名称如 test_db_name ),但在这种情况下,Mongo仍然使用 db_name 运行。我试过:

 导入sys 
从pymongo import连接
从pymongo.errors import ConnectionFailure
从django.db导入连接

try:
connection = Connection(host =localhost,port = 27017)
db_name = connections ['default']。settings_dict [ 'NAME']
db = connection [db_name]
printConnected successfully(Mongo,%s)%(db_name,)
除了ConnectionFailure,e:
sys.stderr .write(无法连接到MongoDB:%s%e)
sys.exit(1)

但不起作用

解决方案

尝试将以下内容放在您的设置文件中:

  import sys 

manage_command = sys.argv [1] if len(sys.argv)> 1 else

TESTING = manage_command.startswith(test)

如果TESTING为真,则使用python manage.py test myapp运行。



编辑:你可能会把它放在任何地方,而不一定是你的设置文件。去吧,看看它是如何工作的!


I'm writing tests in my django project. For now, I have two database connections:

(settings.py)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_name'
        ...
    },
}

and custom connection to MongoDB:

import sys
from pymongo import Connection
from pymongo.errors import ConnectionFailure
try:
    connection = Connection(host="localhost", port=27017)
    db = connection['db_name']
    print "Connected successfully(Mongo, db_name)"
except ConnectionFailure, e:
    sys.stderr.write("Could not connect to MongoDB: %s" % e)
    sys.exit(1)

and I want to know when my project is running through

python manage.py test myapp

Because when you run tests, django automatically create separate DB(with name like test_db_name), but in this case Mongo will still run with db_name. I tried:

import sys
from pymongo import Connection
from pymongo.errors import ConnectionFailure
from django.db import connections

try:
    connection = Connection(host="localhost", port=27017)
    db_name = connections['default'].settings_dict['NAME']
    db = connection[db_name]
    print "Connected successfully(Mongo, %s)" % (db_name,)
except ConnectionFailure, e:
    sys.stderr.write("Could not connect to MongoDB: %s" % e)
    sys.exit(1)

but it does not work

解决方案

Try putting the following in your settings file:

import sys

management_command = sys.argv[1] if len(sys.argv) > 1 else ""

TESTING = management_command.startswith("test")

If TESTING is true you are running with python manage.py test myapp.

edit: You probably could put that anywhere, not necessarily your settings file. Give it a go and see how it works!

这篇关于在Django中如何知道数据库的当前名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆