为什么我的应用引擎查询找不到我的实例?

Bry*_*ock 1 python google-app-engine app-engine-ndb google-cloud-datastore

我一直在玩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)  
Run Code Online (Sandbox Code Playgroud)

我所做的示例查询完全取决于App Engine文档,我做错了什么?

jsp*_*hpl 5

询问

Client.query()返回一个Query对象.

你需要从中得到结果:

query = Client.query( Client.name == 'Bryan Wheelock')
client = query.get() # first result

pprint.pprint(client.name)
Run Code Online (Sandbox Code Playgroud)

要不就:

client = Client.query( Client.name == 'Bryan Wheelock').get()
pprint.pprint(client.name)
Run Code Online (Sandbox Code Playgroud)

ID

id不是模型的属性,而是其关键字的属性.要通过其ID直接获取客户端,您可以执行此操作

client = Client.get_by_id(id)
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看模型方法:https://cloud.google.com/appengine/docs/python/ndb/modelclass