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?
Would like to use sequential order numbers as the key.
Use db.allocate_ids()
as described here to generate unique IDs for your entities.
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)
You might want to look at How to implement "autoincrement" on Google AppEngine where you find a implementation of sequence numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With