Google app engine 在Google App Engine中从祖先路径生成密钥

Google app engine 在Google App Engine中从祖先路径生成密钥,google-app-engine,google-cloud-datastore,Google App Engine,Google Cloud Datastore,我有一个叫请求的模型。使用父用户创建请求,如下所示: request = Request(parentUserKey) 现在,用户的关键名称是该用户的电子邮件,因此当我创建新用户时,我会: user = User(key_name = 'abc@gmail.com') 所以我现在要做的是使用key.from_路径为请求创建一个密钥,所以我尝试: requestKey = Key.from_path('User', 'abc@gmail.com', 'Request', 1) 我之所以使用1

我有一个叫请求的模型。使用父用户创建请求,如下所示:

request = Request(parentUserKey)
现在,用户的关键名称是该用户的电子邮件,因此当我创建新用户时,我会:

user = User(key_name = 'abc@gmail.com')
所以我现在要做的是使用key.from_路径为请求创建一个密钥,所以我尝试:

requestKey = Key.from_path('User', 'abc@gmail.com', 'Request', 1)
我之所以使用1,是因为我将使用此键通过以下方式获取ID大于1(或任何其他任意int)的所有请求:

requestQuery.filter(“\uuuu-key\uuuu>”,requestKey)

然后,出于测试目的,我尝试通过
keyString=str(requestKey)
将键转换为字符串,但出现以下错误:

无法对不完整的密钥进行字符串编码


我做错了什么?

id为0的密钥无效。使用祖先查询代替该过滤器

id为0的密钥无效。使用祖先查询代替该过滤器

要详细说明Guido编写的内容,手动创建密钥可能不是解决问题的最佳方法。相反,若您将用户的所有请求实体存储在用户的实体组中,那个么您可以通过祖先查询简单直接地检索它们

首先,为了使所有请求实体都成为用户的子对象,我们将稍微更改实例化请求对象的方式:

request = Request(parent=parentUser) # Where parentuser is a User already in the datastore
# Do whatever else you need to do to initialize this entity
request.put()
现在,要获取属于用户的所有请求对象,您只需:

requests = Request.all().ancestor(parentUser).fetch(1000)
# Obviously, you want to intelligently manage the condition of having
# more that 1000 Requests using cursors if need be.
拥有使用所有路径信息手动创建关键点的能力是很好的,但这通常比必要的工作要多


这能解决您的用例吗

要详细说明Guido编写的内容,手动创建密钥可能不是解决问题的最佳方法。相反,若您将用户的所有请求实体存储在用户的实体组中,那个么您可以通过祖先查询简单直接地检索它们

首先,为了使所有请求实体都成为用户的子对象,我们将稍微更改实例化请求对象的方式:

request = Request(parent=parentUser) # Where parentuser is a User already in the datastore
# Do whatever else you need to do to initialize this entity
request.put()
现在,要获取属于用户的所有请求对象,您只需:

requests = Request.all().ancestor(parentUser).fetch(1000)
# Obviously, you want to intelligently manage the condition of having
# more that 1000 Requests using cursors if need be.
拥有使用所有路径信息手动创建关键点的能力是很好的,但这通常比必要的工作要多


这能解决您的用例吗

0是任意的。我只是为了这个问题才把它放在那里的。我会将其更改为1,但0和1之间存在很大差异。对于0,str(key)将引发一个异常,并显示您引用的消息。对于1,它将返回有效的编码密钥。0是任意的。我只是为了这个问题才把它放在那里的。我会将其更改为1,但0和1之间存在很大差异。对于0,str(key)将引发一个异常,并显示您引用的消息。对于1,它将返回一个有效的编码密钥。