开发者

Change|Assign parent for the Model instance on Google App Engine Datastore

开发者 https://www.devze.com 2023-01-02 22:17 出处:网络
Is it possible to change or assign new parent to the Model insta开发者_Go百科nce that already in datastore? For example I need something like this

Is it possible to change or assign new parent to the Model insta开发者_Go百科nce that already in datastore? For example I need something like this

task = db.get(db.Key(task_key))
project = db.get(db.Key(project_key))
task.parent = project
task.put()

but it doesn't works this way because task.parent is built-in method. I was thinking about creating a new Key instance for the task but there is no way to change key as well.

Any thoughts?


According to the docs, no:

The parent of an entity is defined when the entity is created, and cannot be changed later.

...

The complete key of an entity, including the path, the kind and the name or numeric ID, is unique and specific to that entity. The complete key is assigned when the entity is created in the datastore, and none of its parts can change.

Setting a parent entity is useful when you need to manipulate the parent and child in the same transaction. Otherwise, you're just limiting performance by forcing them both to be in the same entity group, and restricting your ability to update the relationship after the entity has been created.

Use a ReferenceProperty instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号