Is it possible to change or assign new parent to the Model instance 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With