【发布时间】:2016-04-16 17:34:23
【问题描述】:
我正在尝试使用 mongodb 处理旧版本的 django
Django==1.5.11
django-crispy-forms==1.5.2
django-ifc-rwfm==0.1
django-mongodb-engine==0.6.0
django-registration-redux==1.2
djangotoolbox==1.8.0
pymongo==3.2.2
我正在尝试连接到数据库
但它返回:TypeError at /
“SimpleLazyObject”对象不可调用
这是我的 __init__.py 文件
from django.conf import settings
from django.utils.functional import SimpleLazyObject
from pymongo import MongoClient
_connection = None
def get_connection():
global _connection
if not _connection:
_connection = MongoClient(
host=getattr(settings, 'MONGODB_HOST', None),
port=getattr(settings, 'MONGODB_PORT', None)
)
username = getattr(settings, 'MONGODB_USERNAME', None)
password = getattr(settings, 'MONGODB_PASSWORD', None)
db = _connection[settings.MONGODB_DATABASE]
if username and password:
db.authenticate(username, password)
return db
return _connection[settings.MONGODB_DATABASE]
MongoClient = SimpleLazyObject(get_connection)
def get_collection(collection_name):
return getattr(MongoClient, collection_name)
我是 django 新手,在此先感谢。
【问题讨论】:
标签: python django mongodb django-nonrel