生成唯一编号序列,以用作app引擎数据存储的实体密钥

Ron*_*han 6 python google-app-engine google-cloud-datastore

有没有人有任何示例代码用于创建唯一的数字序列,以用作Google应用引擎数据存储区中的实体的键?

想使用顺序订单号作为关键.

Dav*_*ill 6

使用此处db.allocate_ids()所述为您的实体生成唯一ID.

以下是从上述链接示例中获得的快速示例:

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()
Run Code Online (Sandbox Code Playgroud)

(问题2970参考)