在 Google App Engine 中,如何创建具有特定 ID 的模型?

发布于 2024-12-28 16:11:54 字数 520 浏览 4 评论 0原文

我已将模型的所有实体备份到 CSV 文件中。我正在将实体恢复到本地 dev_server,并希望使用 csv 文件中的 ID 重新创建实体(类似于bulkloader 的做法)。如何在创建语句中传递新实体所需的 ID?

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

当我调用 put() 时,我应该向 Player() 添加什么来创建带有player.key().id()==1234 的播放器?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-04 16:11:54

首先,您需要使用 allocate_id_range 分配 ID 范围,以确保保留 ID对于那些实体。

然后手动构建密钥,并将其传递给 Player 构造函数:

k = Key.from_path('Player', playerID)
player = Player(key = k, ...)
player.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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文