Chris
Chris

Reputation: 4317

In Google App Engine, how do I create a model with a specific ID?

I have backed up all of the entities for a model in a CSV file. I am recovering the entities to my local dev_server and would like to recreate the entities with an ID in the csv file (similar to how bulkloader does). How do I pass in the desired ID for my new entity in my create statement?

playerID = 1234
player = Player(created = datetime.datetime(2012, 1, 25, 9, 20, 5, 757227), 
                nickname = u'chris', 
                email = u'[email protected]')
player.put()

What do I add to Player() to create the player with player.key().id()==1234 when I call put()?

Upvotes: 3

Views: 107

Answers (1)

proppy
proppy

Reputation: 10504

First you need to allocate the id range using allocate_id_range, to make sure to reserve the ids for those entities.

And then just build the key manually, and pass it to Player constructor:

k = Key.from_path('Player', playerID)
player = Player(key = k, ...)
player.put()

Upvotes: 5

Related Questions