Google App Engine NDB自定义键ID [英] Google App Engine NDB custom key id

查看:137
本文介绍了Google App Engine NDB自定义键ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用ndb的方法创建一个对象时,它会自动创建键类型为Key(kind,id)的键,其中id是一个数字。在整个文档中,它显示你可以使用字符串作为密钥的ID,但我无法找到如何在创建对象时自动执行此操作。

我有一个用户模型,并且我正在考虑使用用户的用户名(因为它的唯一性)作为密钥的ID来加快检索速度。这是一个好主意吗?我用户名有什么问题,因为它是用户submited(我正在验证输入)?

解决方案

  class UserModel(ndb.Model):
...

user_model_entity = UserModel(id ='some_string',...)

如果这些ID可能会改变,这可能是一个坏主意。如果这是你自己的系统,你可以对潜在的变化做出反应,这是一个好主意,但是在决定使用它们之前,你需要确保这些ID是唯一且相对稳定的。


When I create an object with ndb's method put it creates the key automatically of the type Key(kind, id) where id is a number. All over the documentation it shows that you can use a string for the key's id but I couldn't find out how to do this automatically when an object is created.

I have a User model and I was thinking to use the user's username (since its unique) as the key's id for faster retrieval. Is that even a good idea? Would I have any problems with the username since it's user submited (i'm validating the input)?

解决方案

class UserModel(ndb.Model):
  ...

user_model_entity = UserModel(id='some_string', ...)

If these IDs are subject to change, this may be a bad idea. If it's your own system and you can react to potential changes, it is a fine idea, but you need make sure the IDs will be unique and relatively stable before deciding to use them.

这篇关于Google App Engine NDB自定义键ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆