Google App Engine NDB自定义密钥ID

时间:2021-06-14 19:16:29

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.

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

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)?

我有一个用户模型,我正在考虑使用用户的用户名(因为它的唯一)作为密钥的ID,以便更快地检索。这是一个好主意吗?我是否会对用户名有任何问题,因为它是用户提交的(我正在验证输入)?

3 个解决方案

#1


30  

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.

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

#2


13  

You specify the id of the entity at the time of creation. When you define the model, you don't set an id attribute there. Thus, for example you have:

您在创建时指定实体的ID。定义模型时,不要在其中设置id属性。因此,例如你有:

class User(ndb.Model):
    # fields here

When you create the model, you have:

创建模型时,您有:

user = User(id='username', ...)

Since the username is unique and you validate your input, then you will not have any problems with this approach.

由于用户名是唯一的,并且您验证了输入,因此您不会遇到任何问题。

For more information about an ndb Model constructor, you can take a look at NDB Model Class - Constructor.

有关ndb Model构造函数的更多信息,您可以查看NDB Model Class - Constructor。

Hope this helps.

希望这可以帮助。

#3


0  

You can also supply integer ID (not necessarily a string) for your model entity.

您还可以为模型实体提供整数ID(不一定是字符串)。

class User(ndb.Model):
...

user = User(id=1234567890, ...)
user.put()

#1


30  

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.

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

#2


13  

You specify the id of the entity at the time of creation. When you define the model, you don't set an id attribute there. Thus, for example you have:

您在创建时指定实体的ID。定义模型时,不要在其中设置id属性。因此,例如你有:

class User(ndb.Model):
    # fields here

When you create the model, you have:

创建模型时,您有:

user = User(id='username', ...)

Since the username is unique and you validate your input, then you will not have any problems with this approach.

由于用户名是唯一的,并且您验证了输入,因此您不会遇到任何问题。

For more information about an ndb Model constructor, you can take a look at NDB Model Class - Constructor.

有关ndb Model构造函数的更多信息,您可以查看NDB Model Class - Constructor。

Hope this helps.

希望这可以帮助。

#3


0  

You can also supply integer ID (not necessarily a string) for your model entity.

您还可以为模型实体提供整数ID(不一定是字符串)。

class User(ndb.Model):
...

user = User(id=1234567890, ...)
user.put()