Question

I want to get user ID 5005 for myself. I read that with GAE you can allocate an ID and set it but I don't fully understand the documentation.

The code is

handmade_key = db.Key.from_path('MyModel', 1)
first_batch = db.allocate_ids(handmade_key, 10)
first_range = range(first_batch[0], first_batch[1] + 1)

my_id = first_range.pop(0)
new_key = db.Key.from_path('MyModel', my_id)
new_instance = MyModel(key=new_key)
new_instance.put()
assert new_instance.key().id() == my_id

But where do I put that I want 5005 as the user ID? The model is the user model from webapp2.

Was it helpful?

Solution

You can allocate specific ids range using allocate_id_range, and then you should be able to construct the Key manually using Key.from_path by supplying your own id, and assign it to your new Model instance using the key argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top