"working outside of application context" 在 Google App Engine 中 Python 远程 API

"working outside of application context" in Google App Engine with Python remote API

我在 Google App Engine 上部署了一个简单的项目,该项目正在运行。之前我能够通过远程 API 控制台从数据存储中获取用户列表,但现在这不再有效。要启动远程 API 控制台,我以这种方式连接(运行ning 来自项目目录):

../../+/google_appengine_PYTHON_SDK/remote_api_shell.py -s project.appspot.com

我发现加载所有模块的最简单方法是 运行:

s~project> help()
help>modules
help>quit

之后我尝试获取用户列表:

s~project> from app.models import User
s~project> User.query().fetch()

最后一行的结果是:

WARNING:root:suspended generator _run_to_list(query.py:964) raised RuntimeError(working outside of application context)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/utils.py", line 142, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/query.py", line 1187, in fetch
    return self.fetch_async(limit, **q_options).get_result()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 325, in get_result
    self.check_success()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/query.py", line 964, in _run_to_list
    batch = yield rpc
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 454, in _on_rpc_completion
    result = rpc.get_result()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_query.py", line 3014, in __query_result_hook
    self.__results = self._process_results(query_result.result_list())
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_query.py", line 3047, in _process_results
    for result in results]
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_rpc.py", line 185, in pb_to_query_result
    return self.pb_to_entity(pb)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/model.py", line 662, in pb_to_entity
    entity = modelclass._from_pb(pb, key=key, set_key=False)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/model.py", line 3119, in _from_pb
    ent = cls()
  File "app/models.py", line 68, in __init__
    if self.email == current_app.config['MAIL_ADMIN']:
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

我对 python 很陌生。我找到 http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html 但无法从中理解如何开始在应用程序上下文中工作。

我试过 linux 和 python 版本:

我已经尝试使用 google 应用引擎 SDK 版本:

问题出在用户 class 本身。我有:

def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        #  FLASKY_ADMIN configuration variable, so as soon as that email address appears in a registration request it can be given the correct role
        if self.role is None:
            print ("there is no role for this user")
            if self.email == current_app.config['MAIL_ADMIN']:
                self.role = ndb.Key('Role', 'administrator')

有问题的行是这一行:

if self.email == current_app.config['MAIL_ADMIN']:

我不知道如何让它与这条线一起工作,所以我删除了它,现在用户被找回了。