仅从App Engin中的ReferenceProperty获取密钥/id

2024-06-23 18:47:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在AppEngine land帮点忙。。。在

使用[Python]API,我从文档中创建如下示例所示的关系:

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

class Story(db.Model):
    author = db.ReferenceProperty(Author)

story = db.get(story_key)
author_name = story.author.name

据我所知,该示例将执行两个数据存储查询。一个取故事,另一个尊重作者,以获取名字。但我希望能够获取身份证,所以请执行以下操作:

^{pr2}$

我只想从引用中得到id。我不想让ReferenceProperty值(因此查询数据存储)。在

从文件中可以看出

the value of a ReferenceProperty is a Key

这使我认为可以对引用的值调用.id()。但它也说:

The ReferenceProperty model provides features for Key property values such as automatic dereferencing.

我找不到任何东西来解释这个引用发生在什么时候?
对ReferenceProperty的值调用.id()是否安全?
是否可以假定调用.id()不会导致数据存储查找?在


Tags: 数据keynameid示例dbmodelclass
1条回答
网友
1楼 · 发布于 2024-06-23 18:47:00

回答我自己的问题是为了帮助其他搜索者。。。

就像怀疑的那样故事.author.key().id()甚至故事.author.id()将导致数据存储查询。正确的方法dictated by the API docs是:

story = db.get(story_key)
author_id = Story.author.get_value_for_datastore(story).id()

相关问题 更多 >

    热门问题