我在Google App Engine上部署了一个简单的项目,项目正在运行。之前我能够通过远程API控制台从数据存储区获取用户列表,但现在这已不再适用。要启动远程API控制台,我将以这种方式连接(从项目目录运行):
../../+/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版本:
我尝试使用谷歌应用引擎SDK版本:
答案 0 :(得分:0)
问题出在User类本身。我有:
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']:
我不知道如何设法使其与此行一起使用,因此我已将其删除,现在已检索到该用户。