我一直在玩App Engine,但我似乎误解了NDB数据存储区查询。 我把抛出的错误放在查询旁边。
在交互式控制台中玩游戏:
from google.appengine.ext import ndb
class Client(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
#instantiated client instance with the parameters below. ID is 6578378068983808
#client = Client(email = "bryan@gmail.com", name = "Bryan Wheelock" ).put()
client = Client.query( Client.name == 'Bryan Wheelock')
#client = Client.query( Client.ID == 6578378068983808 ) #AttributeError: type object 'Client' has no attribute 'ID'
#client = Client.all() #AttributeError: type object 'Client' has no attribute 'all'
#client = Client.get_by_id(6578378068983808) #THIS WORKS returns u'Bryan Wheelock'
pprint.pprint(client.name)
我所做的示例查询完全取决于App Engine文档,我做错了什么?
答案 0 :(得分:5)
Client.query()返回一个Query对象。
你需要从中得到结果:
query = Client.query( Client.name == 'Bryan Wheelock')
client = query.get() # first result
pprint.pprint(client.name)
或只是:
client = Client.query( Client.name == 'Bryan Wheelock').get()
pprint.pprint(client.name)
id不是模型的属性,而是其键的属性。要通过其ID直接获取客户端,您可以执行
client = Client.get_by_id(id)
供您参考,您可以在此处查找模型方法:https://cloud.google.com/appengine/docs/python/ndb/modelclass