有没有办法让 GAE 数据存储实体同时拥有 key_name 和 id?
除了我生成的 key_name
之外,我还希望有一些其他属性,它将充当 id (我不想向用户显示 key_name
) 。可以是id
吗?或者,如何生成唯一值而不是id
?
我将做什么 - 我将使用该 id 和父键名称生成一个 url。如果用户单击此链接,我需要找到此数据存储实体并更新它。
这是当前的代码。
创建记录:
item = Items.get_by_key_name(key_names=user_id, parent=person)
if item is None:
item = Items(key_name='id'+user_id, parent=person)
获取记录:
item = Items.get_by_key_name(key_names=user_id, parent=person)
user_id
是应该隐藏的内容。
In addition to the key_name
I generate, I also would like to have some other property, which will act as id (I don't want to show key_name
to the user). Can it be id
? Or, how to generate unique value instead of id
?
What I will do - I will generate a url with usage of that id and parent key name. If user clicks on this link, I'll need to find this datastore entity and update it.
Here is the current code.
Creation of the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
if item is None:
item = Items(key_name='id'+user_id, parent=person)
Getting the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
user_id
is what should be hidden.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能是错的,因为您的要求不清楚,但对我来说,您应该使用以下方法仅将密钥传递给视图:
然后您可以将密钥传回控制器并轻松检索给定的实体:
I could be probably wrong because your requirements are not clear, but for me you should pass just the key to the view using:
then you could pass back the key to the controller and easily retrieve a given entity with:
实体只有键名或 ID 之一,而不是两者兼而有之。您可以创建一个具有指向目标实体的单个 ReferenceProperty 的实体,并使用其 ID 作为标识符,但确实没有理由不向用户透露键名称 - 精心编写的应用程序不应依赖于该值仍然是秘密。
请注意,从字符串编码的密钥中提取密钥名称(以及有关密钥的其余信息)非常容易。
Entities have exactly one of a key name or ID - never both. You could create an entity with a single ReferenceProperty pointing to your target entity, and use its ID as an identifier, but there really should be no reason not to reveal a key name to a user - a well authored app should not rely on this value remaining secret.
Note that it's trivially easy to extract the key name (and the rest of the information about a key) from the string encoded key.