[英]Generating unique number sequence for use as entity key for app engine datastore
Has anyone got any example code for creating a unique number sequence to be used as keys for an entity in a Google app engine datastore? 有没有人有任何示例代码用于创建唯一的数字序列,以用作Google应用引擎数据存储区中的实体的键?
Would like to use sequential order numbers as the key. 想使用顺序订单号作为关键。
Use db.allocate_ids()
as described here to generate unique IDs for your entities. 使用此处所述的
db.allocate_ids()
为您的实体生成唯一ID。
Here's a quick example derived from the example at the above link: 以下是从上述链接示例中获得的快速示例:
from google.appengine.ext import db
# get unique ID number - I just get 1 here, but you could get many ...
new_ids = db.allocate_ids(handmade_key, 1)
# db.allocate_ids() may return longs but db.Key.from_path requires an int (issue 2970)
new_id_num = int(new_id[0])
# assign the new ID to an entity
new_key = db.Key.from_path('MyModel', new_id_num)
new_instance = MyModel(key=new_key)
...
new_instance.put()
( issue 2970 reference ) ( 问题2970参考 )
您可能想看看如何在Google AppEngine上实现“自动增量” ,您可以在其中找到序列号的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.