python - GAE NDB AttributeError 模型实例没有属性

标签 python google-app-engine google-cloud-datastore google-cloud-endpoints

我有一个名为“Bin”的 Google App Engine NDB 模型。 我已修改“Bin”的属性并删除了与旧模型对应的所有数据条目。我的目标是查询所有 bin 并访问 GAE Endpoints 函数内的属性“标签”。但是,当查询和迭代返回的列表时,我得到 AttributeError: 'Bin' object has no attribute 'label' ,这显然是错误的。日志输出显示返回的列表实际上包含两个具有名为“label”属性的 Bin 对象。

有趣的是,当我更改查询周围的代码时,该代码将在下次运行时正常工作。然后又失败了。

我已清除内存缓存,重新启动应用程序引擎启动器并删除所有条目(然后添加新条目)以尝试删除任何缓存。

NDB模型

class Bin(ndb.Model):
    id=ndb.IntegerProperty(required=True)
    label=ndb.StringProperty(required=True)
    items=ndb.JsonProperty()

查询

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from includes.models import Bin
...
@endpoints.api(name='iwora', version='v1')
class Api(remote.Service):
    @endpoints.method(message_types.VoidMessage, BinCollection,
    path='bin', http_method='GET',
    name='bin.all')
    def bin_all(self, request):
        self.user_required()

        namespace = namespace_manager.get_namespace()
        try:
            namespace_manager.set_namespace(self.user.active_org)
            bins = Bin.query().fetch()
            logging.info('BINS: %s' % bins)
            binsresponse = []
            for bin in bins:
                logging.info('BIN: %s' % bin)
                obj = {
                    'label': bin.label # error happens here
                }
                binsresponse.append(obj)
        finally:
            namespace_manager.set_namespace(namespace)

        return BinCollection(bins=binsresponse)
...

当查询运行并且我尝试迭代返回的垃圾箱时,我得到以下输出:

BINS: [Bin(key=Key('Bin', 'default_bin', 'Bin', 5275456790069248), id=2L, items=None, label='1-1-2'), Bin(key=Key('Bin', 'default_bin', 'Bin', 5838406743490560), id=1L, items=None, label='1-1-1')] BIN: Bin(key=Key('Bin', 'default_bin', 'Bin', 5275456790069248), id=2L, items=None, label='1-1-2') Encountered unexpected error from ProtoRPC method implementation: AttributeError ('Bin' object has no attribute 'label') Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app response = method(instance, request) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote return remote_method(service_instance, request) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method response = method(service_instance, request) File "/Users/rlfrahm/Apps/iwora/api.py", line 524, in bin_all 'label': bin.label AttributeError: 'Bin' object has no attribute 'label'

我认为(正如我相信,但尚未证实),只有当我实现了模型,将条目添加到数据库中,然后通过更改属性名称或添加新属性来修改模型时,才会发生这种情况。即使删除条目后我仍然遇到这个问题。我意识到这个问题在当前状态下是不可重复的,因为我不完全知道如何重现该错误。这似乎是“刚刚发生的”。我还有许多其他函数使用相同的过程并且工作得很好。甚至 SDK 控制台中的交互式控制台也能正确返回。

因此,我真的想知道是否有人见过这种类型的行为或发现我的流程有明显的错误?

最佳答案

这是我试图重现您的错误的最佳尝试 - 它包括您打算与我们分享的所有稀疏代码片段,但不幸的是根本没有显示任何错误。

import logging
import webapp2
from google.appengine.ext import ndb

class Bin(ndb.Model):
    id=ndb.IntegerProperty(required=True)
    label=ndb.StringProperty(required=True)
    items=ndb.JsonProperty()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        bins = Bin.query().fetch()
        if not bins:
            Bin(id=1L, items=None, label='1-1-1').put()
            Bin(id=2L, items=None, label='1-1-2').put()
            bins = Bin.query().fetch()
        logging.info('BINS: %s' % bins)
        binsresponse = []
        for bin in bins:
            logging.info('BIN: %s' % bin)
            obj = {
                'label': bin.label # error happens here
            }
            binsresponse.append(obj)

        self.response.write('Hello world! ')
        self.response.write(binsresponse)


app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

在 SDK 中本地运行它会显示出预期的结果:

Hello world! [{'label': u'1-1-2'}, {'label': u'1-1-1'}]

日志中的相关片段是:

INFO     2014-12-21 03:15:56,721 main.py:19] BINS: [Bin(key=Key('Bin', 5066549580791808), id=2, items=None, label=u'1-1-2'), Bin(key=Key('Bin', 5629499534213120), id=1, items=None, label=u'1-1-1')]
INFO     2014-12-21 03:15:56,721 main.py:22] BIN: Bin(key=Key('Bin', 5066549580791808), id=2, items=None, label=u'1-1-2')
INFO     2014-12-21 03:15:56,722 main.py:22] BIN: Bin(key=Key('Bin', 5629499534213120), id=1, items=None, label=u'1-1-1')

IOW,没有任何问题。

添加到这个小代码(其中确实包括您打算发布的最后一点!-)中缺少的内容,以使其重现您的错误,并编辑您的问题以包含最少的失败示例-- 否则,放弃所有希望任何人能够帮助您纠正错误的希望!-)

关于python - GAE NDB AttributeError 模型实例没有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584206/

相关文章:

python - bash:mkvirtualenv:找不到命令

python - Flask-login current_user 只返回 ID

google-app-engine - 处理 appengine 中的子域

java - 从 GAE 应用程序内部写入 PDF

python - 从 SQL 中按周对 Pandas 进行分组

python - 在 sqlalchemy ilike 语句中使用变量

mysql - 如何使用 App Engine 从 eclipse 连接到 google cloud sql 实例?

google-app-engine - Google App Engine 数据存储区的 ListProperty 的最大大小/限制是多少?

python - 自 1.8.2 更新后出现错误

google-app-engine - 在多个区域部署单个 GAE 应用程序