views:

69

answers:

1

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?

+4  A: 

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.

Drew Sears
Yeah, looks like I have no choice :) thank you.
Vladimir Prudnikov
BTW, here is another good usage of the parents http://www.youtube.com/watch?v=AgaL6NGpkB8 in the Microblogging example.
Vladimir Prudnikov