简体   繁体   English

使用Appengine的NDB从实体的id获取密钥

[英]Getting key from the id of the entity using Appengine's NDB

I have a model based on ndb, while saving it, I stored 'id' field with current logged-in user's user id. 我有一个基于ndb的模型,在保存它的同时,我使用当前登录用户的用户ID存储了'id'字段。 (Why I am doing this? Actually this model used to be based on db.Model and key_name has this user's id. Now, I am converting it to ndb) (为什么我这样做?实际上这个模型过去基于db.Model而key_name有这个用户的id。现在,我将它转换为ndb)

m= Modelclass(id = str(users.get_current_user().used_id()),
      --- Other fields ---
m.put()

This model's edit form sends this 'id' and I wanted to get corresponding 'key' from it. 这个模型的编辑表单发送了这个'id',我希望从中得到相应的'key'。 But, I got "Key id number is too long; received 'some big_number'". 但是,我得到“密钥ID号太长;收到'一些big_number'”。 Tried both ways 试过两种方式

Modelclass.get_by_id(<id>).key 

OR 要么

ndb.Key('Modelclass', <id>)

This is one case, there may be other cases where user can store some big number in 'id' field. 这是一种情况,可能存在用户可以在“id”字段中存储一些大数字的其他情况。 In these scenarios, we can't extract key from 'id'. 在这些情况下,我们无法从“id”中提取密钥。 So, how to solve such a problem. 那么,如何解决这样的问题。

I am new to ndb. 我是ndb的新手。 Thanks for any help. 谢谢你的帮助。

Looks like your value is an int, not a string. 看起来你的值是一个int,而不是一个字符串。 But you converted it into a string when creating the entitiy. 但是在创建权利时,您将其转换为字符串。 There's a simple solution: 有一个简单的解决方案:

ndb.Key('Modelclass', str(<id>))

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM