在烧瓶应用程序中,我使用MongoEngine作为ORM。 我正在主python文件中定义db对象,并试图通过从那里导入它来从不同的类重用它。
这是我在主py文件中创建db
的方式:
db = MongoEngine(app, config={
'db': "MY_DB",
'host': "myhost",
'port': 27017,
'username': 'user',
'password': 'password'
})
这是我试图在另一个类中进行的操作(正在从rest入口点使用其实例):
from app.engine import db
class ReportService:
def __init__(self):
pass
@staticmethod
def download_raw_data(collection_name, _from, _to):
logger.info('Downloading raw_data from collection: ' + collection_name)
query = {
"date": {"$gte": _from, "$lte": _to}
}
result = db[collection_name].find(query)
return result
但是,调用该入口点时出现此错误:
[2019-02-21 10:52:22 +0000] [12] [ERROR] Error handling request module/download/rawdata
Traceback (most recent call last):
File "/app/env/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 56, in handle
self.handle_request(listener_name, req, client, addr)
File "/app/env/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 107, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/app/env/lib/python3.6/site-packages/flask_socketio/__init__.py", line 43, in __call__
start_response)
File "/app/env/lib/python3.6/site-packages/engineio/middleware.py", line 67, in __call__
return self.wsgi_app(environ, start_response)
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/app/env/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/app/env/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/app/env/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/app/env/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/app/env/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/controllers/modules.py", line 51, in download_raw_data
result = reports.download_raw_data(collection_name, start_time, end_time)
File "/app/service/services.py", line 114, in download_raw_data
result = db[collection_name].find(query)
TypeError: 'MongoEngine' object is not subscriptable
我在做什么错?我也想了解为什么会发生此错误。
预先感谢
答案 0 :(得分:0)
Mongoengine(...)
返回的内容不是立即成为pymongo数据库实例,如果需要,您必须使用get_db()
:
me = MongoEngine(app, config={...})
...
db = mongo.get_db()
db[collection_name].find(query)
我同意docs令人困惑,因为它称为db
,这令人产生误解...