【发布时间】:2011-02-19 00:51:11
【问题描述】:
有没有人有任何示例代码来创建一个唯一的数字序列以用作 Google 应用引擎数据存储中实体的键?
希望使用顺序订单号作为关键字。
【问题讨论】:
标签: python google-app-engine google-cloud-datastore
有没有人有任何示例代码来创建一个唯一的数字序列以用作 Google 应用引擎数据存储中实体的键?
希望使用顺序订单号作为关键字。
【问题讨论】:
标签: python google-app-engine google-cloud-datastore
您可能想查看How to implement "autoincrement" on Google AppEngine,您可以在其中找到序列号的实现。
【讨论】:
按照here 的描述使用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()
【讨论】: