Create a unique sequence of numbers to use as an entity key for the application data store

Does anyone have any sample code to create a unique numerical sequence that will be used as keys for an object in the data warehouse of a Google application?

I would like to use serial numbers as a key.

+5
source share
2 answers

Use db.allocate_ids()as described here to generate unique identifiers for your objects.

Here is an example in the example from the link above:

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

( link 2970 link )

+5
+2

All Articles