Search code examples
google-app-enginereferenceproperty

Get the string-encoded key of an entity from a reference property in appengine


In order to get the string-encoded key of an entity, I just do the following:

key = entity.key()
string_encoded_key = str(key)

I have a reference to another entity via the ReferenceProperty.

class ParentClass(db.Model):
name = db.StringProperty()

class ChildClass(db.Model):
name = db.StringProperty()
bio_parent = db.ReferenceProperty(ParentClass)


johnnys_parent = ParentClass(name="John").put()
child = ChildClass(name="Johnny",bio_parent=johnnys_parent).put()

#getting the string-encoded key of the parent through the child
child = ChildClass.all().filter("name","Johnny").get()
string_encoded_key = str(child.bio_parent) # <--- this doesn't give me the string-encoded key

How do I get the string-encoded key of the biological parent through the child entity without fetching the parent entity?

Thanks!


Solution

  • You can get the key of a reference property without fetching it like this:

    ChildClass.bio_parent.get_value_for_datastore(child_instance)
    

    From there, you can fetch the string encoded form as you normally would.