【问题标题】:Generating unique number sequence for use as entity key for app engine datastore生成唯一编号序列以用作应用引擎数据存储的实体键
【发布时间】:2011-02-19 00:51:11
【问题描述】:

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

希望使用顺序订单号作为关键字。

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore


    【解决方案1】:

    您可能想查看How to implement "autoincrement" on Google AppEngine,您可以在其中找到序列号的实现。

    【讨论】:

      【解决方案2】:

      按照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()
      

      (issue 2970 reference)

      【讨论】:

      • 或者不指定key,让datastore为你生成一个,效果完全一样。
      • @NickJohnson,在 Google App Engine 1.8.1 版本中是 no longer true
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多