如何在实体创建后但放入数据存储之前添加/更改其父级?

2024-04-26 06:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

从我在文档中读到的内容来看,将实体放入数据存储后,不可能更改其父级。但我正在寻找一种方法,在发生这种情况之前(但在创建之后)更改父级。所以与其这样:

John = Student(parent=BlueClassroom.key, name="John", last_name="Smith")
John.put()

我在找这样的东西:

John = Student(name="John", last_name="Smith")
John.parent = BlueClassroom.key
John.put()

现在,第一个可行,但第二个不行(它只是忽略了第二行)。我也尝试过使用populate,但这只适用于常规属性。有什么办法吗


Tags: 数据方法keyname文档实体内容put
1条回答
网友
1楼 · 发布于 2024-04-26 06:49:19

根据NDB Model Class Constructor文件:

You cannot easily define a property named "key", "id", "parent", or "namespace". If you pass, for example, key="foo" in a constructor or populate() call, it sets the entity's key, not a property attribute named "key".

我建议在准备创建实体之前,将数据作为dict传递:

john = {name="John", last_name="Smith"}
...
John = Student(parent=BlueClassroom.key)
John.populate(john)

相关问题 更多 >