GAE 数据存储在 python api 中预先加载

发布于 2024-08-18 14:56:36 字数 834 浏览 6 评论 0原文

我有两个一对多关系的模型:

类问题(db.Model):

<块引用>

questionText = db.StringProperty(multiline=False)

类答案(db.Model):

<块引用>

answerText = db.StringProperty(multiline=False)

question = db.ReferenceProperty(Question, collection_name='answers')

我在 Flex 中实现了前端并使用 pyamf 来加载数据。

当我尝试加载所有包含相关问题的答案时,所有内容都按预期工作,我可以访问 场地

回答问题

但是,在加载问题的情况下(例如通过 Questions.all() ),'question.answers' 保持为空/空

(尽管在服务器/python 端,我可以毫无问题地修改 Question.answers - 可能在延迟加载之后) 。

那么是否可以加载所有问题以及答案?

(我知道这在 JPA Java api 中是可能的,但是 python 呢?)

我应该使用额外的设置、GQL 查询或 django 框架来使其工作吗?

I have two models in relation one-to-many:

class Question(db.Model):

questionText = db.StringProperty(multiline=False)

class Answer(db.Model):

answerText = db.StringProperty(multiline=False)

question = db.ReferenceProperty(Question, collection_name='answers')

I have front-end implemented in Flex and use pyamf to load data.

When i try to load all answers with related questions all works as desired and I can access
field

answer.question

however in case of loading questions (e.g. by Questions.all() ), 'question.answers' remains empty/null

(though on server/python side I can revise question.answers without problem - probably after lazy-loading).

So is it possible to load all questions along with answers ?

(I know this is possible in JPA Java api but what about python ?)

Shoud I use additional setting, GQL query or django framework to make it work ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-08-25 14:56:36

默认情况下,PyAMF 不会对 ReferenceProperty 字段进行编码,除非它们已由服务方法专门加载。这是有意为之的,这样您最终就不会进行不必要的编码。

PyAMF 查找特殊的类属性 __amf__,它用于自定义该类型实例的编码和解码过程。更多信息可以在文档中找到。

因此,要强制对所有问题的所有答案进行编码,您应该能够执行以下操作:

class Question(db.Model):
    class __amf__:
        static = ('answers',)

    questionText = db.StringProperty(multiline=False)

class Answer(db.Model):
    answertText = db.StringProperty(multiline=False)
    question = db.ReferenceProperty(Question, collection_name='answers')

设置静态属性将确保每个 Question 实例都设置了 answers 属性(通过getattr),这将依次进行您需要的数据存储查找。

重要的是,此设置不适用于应用程序范围,因此任何问题都将具有出现在实例上的答案属性。

By default PyAMF will not encode ReferenceProperty fields unless they have already been specifically loaded by the service method. This is on purpose so you don't end up encoding more than you have to.

PyAMF looks for a special class attribute __amf__ which it uses to customise the encoding and decoding process for instances of that type. More information can be found in the docs.

So, to force the encoding of all answers for all questions you should be able to do:

class Question(db.Model):
    class __amf__:
        static = ('answers',)

    questionText = db.StringProperty(multiline=False)

class Answer(db.Model):
    answertText = db.StringProperty(multiline=False)
    question = db.ReferenceProperty(Question, collection_name='answers')

Setting the static attribute will ensure that every Question instance has the answers attribute set (via getattr) which will in turn to the datastore lookup that you require.

It is important to not that this setting is application wide, so any question will have an answers attribute as it appears on the instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文