Get the string key of an object from a reference property in appengine

To get the string code of an object, I just do the following:

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

I have a reference to another object via 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 to get a string encoded biological parent key through a child without retrieving the parent?

Thank!

+3
source share
3 answers

You can get the key of the reference property without getting it like this:

ChildClass.bio_parent.get_value_for_datastore(child_instance)

From there, you can get the lowercase encoded form, as usual.

+4
source

parent is the keyword argument in the model class. So when you use

child = Child (name='Johnny', parent=parent)

parent , parent. - .

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

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

johns_parent = ParentClass (name='John Sr.').put ()
john = ChildClass (name='John Jr.', ref=johns_parent).put ()

# getting the string encoded key
children = ChildClass.all ().filter ('name', 'John Jr.').get ()
string_encoded_key = str (children.ref)

. .

:

+1

, .

string_encoded_key = str(child.bio_parent.key())
0

All Articles