简体   繁体   中英

How can I add/change the parent of an entity after it has been created but before it is put in the Datastore?

From what I've read in the docs, it's impossible to change the parent of an entity after it has been put in the datastore. But I am looking for a way to change the parent before that happens (but after it is created). So instead of having this:

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

I am looking for something like this:

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

Now, the first one works, but the second one does not (it just ignores the second line). I have also tried to use populate, but that just works for regular properties. Is there some way to do this?

According to the NDB Model Class Constructor docs:

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".

I'd suggest passing the data around as a dict until you're ready to create the entity:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM