【问题标题】:In Google App Engine, how do I create a model with a specific ID?在 Google App Engine 中,如何创建具有特定 ID 的模型?
【发布时间】:2012-01-25 13:11:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多