В приложении для колб я использую MongoEngine в качестве ORM.Я определяю объект db в своем основном файле python и пытаюсь повторно использовать его из разных классов, импортируя его оттуда.
Вот как я создаю db
в моем основном файле py:
db = MongoEngine(app, config={
'db': "MY_DB",
'host': "myhost",
'port': 27017,
'username': 'user',
'password': 'password'
})
Это то, что я пытаюсь сделать из другого класса (экземпляр которого используется из точки входа отдыха):
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
Что я делаю не так?Я также хотел бы понять, почему происходит эта ошибка.
Заранее спасибо