3

为了获取实体的字符串编码键,我只需执行以下操作:

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

我通过 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

如何在不获取父实体的情况下通过子实体获取生物父的字符串编码键?

谢谢!

4

3 回答 3

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.

于 2011-03-16T17:13:42.370 回答
1

parent是模型类中的关键字参数。所以,当你使用

child = Child (name='Johnny', 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. 模型类
  2. 参考属性
  3. 实体组和祖先路径
于 2011-03-16T08:37:24.967 回答
0

我认为您可以通过这种方式实现。

string_encoded_key = str(child.bio_parent.key())
于 2011-03-16T09:52:54.277 回答