简体   繁体   中英

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'chris@home.com')
player.put()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM