3

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!

Albert
  • 3,611
  • 3
  • 28
  • 52

3 Answers3

4

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.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
1

parent is a keyword argument in the Model Class. So, when you use

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

it refers to the parent of that entity and not the parent attribute. You should change the name of the attribute from parent to something more meaningful and less ambiguous.

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)

The parent of an entity can be assigned only at the time of creation. It is in the full key path of the entity and can not be changed in the entire life of that entity.

Resources :

  1. Model Class
  2. Reference Property
  3. Entity Groups and Ancestor Path
pr4n
  • 2,918
  • 3
  • 30
  • 42
  • Ok, I edited it accordingly. Do you have a suggestion on how I can get the string-encoded key of the biological parent of the child through the childclass entity without fetching the parentclass entity? – Albert Mar 16 '11 at 10:35
0

I think you can achieve this way.

string_encoded_key = str(child.bio_parent.key())
Abdul Kader
  • 5,781
  • 4
  • 22
  • 40
  • I wanted to get the string-encoded key *without* fetching the parentclass entity from the datastore. Your answer fetches it from the datastore first. – Albert Mar 16 '11 at 10:34
  • 1
    Yes, your solution works, but it doesn't fit the requirements. As I stated in my question, I wanted to get it WITHOUT FETCHING the parentclass entity from the datastore. Your solution fetches the parentclass, which I want to avoid. – Albert Mar 16 '11 at 11:55
  • I think it's not possible to do that .In that way, you can change your model to store the key using db.ListProperty(db.Key). To know more about modeling you can have a look here [Appengine Data modeling](http://daily.profeth.de/2008/04/er-modeling-with-google-app-engine.html) – Abdul Kader Mar 16 '11 at 12:26