您如何不自动取消引用Google App Engine中的db.ReferenceProperty? [英] How do you NOT automatically dereference a db.ReferenceProperty in Google App Engine?

查看:92
本文介绍了您如何不自动取消引用Google App Engine中的db.ReferenceProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

  class Foo(db.Model):

bar = db。 ReferenceProperty(Bar)

foo = Foo.all()。get()

有没有办法让我做foo.bar而不用查询数据存储?该文档说 foo.bar 将是一个 Key 的实例,所以我希望能够做到 foo.bar.id()并且能够得到 Bar 的 id code>,它与 foo 相关联,但似乎并不这样。



PS :我所指的文档部分可以在这里找到:



http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ReferenceProperty



,它表示:

一个应用程序可以显式地指定db.get()一个ReferenceProperty的值(它是一个Key)以测试引用的实体是否存在。

解决方案

文档说,


ReferenceProperty值可以是
,就好像它是一个模型实例
,数据存储实体将被获取
,并且模型实例首次以这种方式使用时创建

未触及的引用属性不会
查询不需要的数据。


只要您不要碰它,但如果你这样做,例如如你所示,调用 .id()调用,数据存储实体将被提取,文档说。更多文档此处,但他们不显示了一种神奇的方式来获取id,而无需获取。



我还没有尝试过,但是您可以使用 get_value_for_datastore 属性以获得属性的Python本地值,我相信确实是一个Key - 它的 id()方法应该可以在没有数据存储区提取的情况下调用。你有没有试过?


Suppose I have

class Foo(db.Model):

  bar = db.ReferenceProperty(Bar)

foo = Foo.all().get()

Is there a way for me to do foo.bar without a query being made to Datastore? The docs say that foo.bar will be an instance of Key, so I would expect to be able to do foo.bar.id() and be able to get the id of the Bar that's associated with foo, but it doesn't seem to work that way.

PS: The part of the docs I'm referring to can be found here:

http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ReferenceProperty

and it says this:

"An application can explicitly db.get() the value of a ReferenceProperty (which is a Key) to test whether the referenced entity exists."

解决方案

As the docs say,

The ReferenceProperty value can be used as if it were a model instance, and the datastore entity will be fetched and the model instance created when it is first used in this way. Untouched reference properties do not query for unneeded data.

so you're fine as long as you don't touch it, but if you do though it, e.g. with an .id() call as you show, "the datastore entity will be fetched", the docs say. More docs are here, but they don't show a way to magically get the id without fetching, either.

I have not tried, but you might be able to use the get_value_for_datastore of the property to get "the Python-native value of the property" which I believe is indeed a Key -- whose id() method should be callable without datastore fetches. Have you tried that?

这篇关于您如何不自动取消引用Google App Engine中的db.ReferenceProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆