Change|Assign parent for the Model instance on Google App Engine Datastore
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.
精彩评论