阅读背景:

GAE NDB AttributeError模型实例没有属性

来源:互联网 

I have a Google App Engine NDB model called 'Bin'. I have modified the properties for 'Bin' and deleted all data entries that correspond to the older model. My goal is to query for all bins and access the attribute 'label' inside a GAE Endpoints function. However, when querying and iterating through the returned list I get AttributeError: 'Bin' object has no attribute 'label', which is clearly false. Log output shows that the returned list does in fact contain two Bin objects that have an attribute called 'label'.

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

What is interesting is that when I change code around the query, the code will work properly for the next run. Then it fails again.

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

I have cleared the memcache, restarted the app engine launcher and deleted all entries (then adding new ones) in an attempt to remove any caching.

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

NDB Model

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

The Query

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)
...

When the query is ran and I attempt to iterate through the returned bins I get the following output:

运行查询并尝试迭代返回的bin时,我得到以下输出:

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'

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')ProtoRPC方法实现遇到意外错误:AttributeError('Bin'对象没有属性'label')Traceback(最近一次调用) last):文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py”,第181行,在protorpc_service_app response =方法(实例,请求)文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle / Contents / Resources / google_appengine / lib / endpoints-1.0 / endpoints / api_config.py”,第1332行,在invoke_remote中返回remote_method(service_instance,request)文件“/ Applications / GoogleAppEngineLauncher。 app / Contents / Resources / GoogleAppEngine-default.bundle / Contents / Resources / google_appengine / lib / protorpc-1.0 / protorpc / remote.py“,第412行,在invoke_remote_method response = method(service_instance,request)文件”/ Users / rlfrahm /Apps/iwora/api.py“,第524行,在bin_all'label'中:bin.label AttributeError:'Bin'对象没有属性'label'

I think (as in I believe, but have not confirmed) that this only happens once I have implemented a model, added entries into the db, and then modified the model either by changing a property name or adding a new property. Even after deleting the entries I still experience this issue. I realize that this issue is not repeatable in its current state because I do not exactly know how to reproduce the error. It seems to "just happen." I have many other functions that use this same process and work just fine. Even the interactive console in the SDK Console returns correctly.

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

Therefore, I am really wondering if anyone has seen this type of behavior or sees something obviously wrong with my process?

因此,我真的想知道是否有人看到过这种行为或者看到我的流程明显错误了?

1 个解决方案

#1


2  

Here's my best attempt at trying to reproduce your error -- it includes all the sparse fragments of code you've deigned to share with us, and miserably fails to show any error at all.

这是我尝试重现您的错误的最佳尝试 - 它包括您已经设计与我们共享的所有稀疏代码片段,并且很难显示任何错误。

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)

Running this locally in the SDK shows, as expected:

在SDK中本地运行此按预期显示:

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

and the relevant snippet from the log is:

并且日志中的相关片段是:

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, no problem whatsoever.

IOW,没问题。

Please add to this tiny code (which does include every single last bit you deigned to post!-) what's missing to make it reproduce your error, and edit your question to include the minimum failing example -- or else, give up all hope that anybody will ever be able to help you debug your mistakes!-)

请添加到这个微小的代码(其中包括你要发布的每一个最后一位! - )缺少什么使它重现你的错误,并编辑你的问题以包括最小的失败的例子 - 或者,放弃所有希望任何人都能帮助你调试你的错误! - )


分享到: