Freddy
Freddy

Reputation: 174

Get datastore entity's parent key

I am trying to get the parent key from a certain entity. I have 2 classes, Album and Photo. Album is a parent to photo, so when I upload a photo I assign the key of that certain album it belongs to as a parent to the photo.

album = db.get(self.album_key)
photo = Photo(parent=album)

Problem arises when I try to query for the parent id from photo. The below code just gives me an output of "parent key: <"

photo = db.get(photo_key)
photoparent =  photo.parent

self.response.out.write("parent key: %s" %photoparent)

How can I properly pull the parent key from a Photo instance?

Thanks!

Upvotes: 4

Views: 2486

Answers (1)

Tim Hoffman
Tim Hoffman

Reputation: 12986

parent is a method call, not a property.

So you code should read

photoparent = photo.parent()

See docs https://developers.google.com/appengine/docs/python/datastore/modelclass#Model_parent

Also when the key is output in

self.response.out.write("parent key: %s" %photoparent)

The keys str method outputs a representation of the object with <>'s So you may want to do something with the parent to make it's html output more sane ;-)

Cheers

Tim

Upvotes: 3

Related Questions