python – 只從App Engine中的ReferenceProperty獲取Key / id
我可以在AppEngine土地上使用一點幫助
使用[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
據瞭解,該示例將會產生兩個資料儲存區查詢.一個獲取故事,然後一個尊重作者以訪問名稱.但是我想要獲取id,所以做一些像:
story = db.get(story_key) author_id = story.author.key().id()
我想從引用中獲取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()不會導致資料儲存查詢嗎?
因為懷疑呼叫story.author.key().id()或甚至story.author.id()將導致資料儲存查詢.正確的方法ofollow,noindex" target="_blank">dictated by the API docs 是:
story = db.get(story_key) author_id = Story.author.get_value_for_datastore(story).id()
http://stackoverflow.com/questions/3044121/fetching-just-the-key-id-from-a-referenceproperty-in-app-engine