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

2024-09-30 14:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在玩appengine,但我似乎误解了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)  

我所做的示例查询来自appengine文档,我做错了什么?你知道吗


Tags: nameclientidobjectemailtypequerybryan
1条回答
网友
1楼 · 发布于 2024-09-30 14:28:18

查询

你知道吗客户端.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)

为了便于参考,您可以在这里查找Model方法:https://cloud.google.com/appengine/docs/python/ndb/modelclass

相关问题 更多 >