[英]GAE datastore eager loading in python api
I have two models in relation one-to-many: 我有两个关系一对多的模型:
class Question(db.Model): 类Question(db.Model):
questionText = db.StringProperty(multiline=False) QuestionText = db.StringProperty(multiline = False)
class Answer(db.Model): 类Answer(db.Model):
answerText = db.StringProperty(multiline=False) answerText = db.StringProperty(multiline = False)
question = db.ReferenceProperty(Question, collection_name='answers') 问题= db.ReferenceProperty(问题,collection_name ='答案')
I have front-end implemented in Flex and use pyamf to load data. 我在Flex中实现了前端,并使用pyamf加载数据。
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 (eg by Questions.all() ), 'question.answers' remains empty/null 但是,在加载问题的情况下(例如,通过Questions.all()),“ question.answers”保持为空/空
(though on server/python side I can revise question.answers without problem - probably after lazy-loading). (尽管在服务器/ python端,我可以毫无问题地修改question.answers-可能是在延迟加载之后)。
So is it possible to load all questions along with answers ? 那么是否可以同时加载所有问题和答案?
(I know this is possible in JPA Java api but what about python ?) (我知道这在JPA Java API中是可能的,但是python呢?)
Shoud I use additional setting, GQL query or django framework to make it work ? 我应该使用其他设置,GQL查询或Django框架来使其正常工作吗?
By default PyAMF will not encode ReferenceProperty fields unless they have already been specifically loaded by the service method. 默认情况下,PyAMF不会对ReferenceProperty字段进行编码,除非已由service方法专门加载了它们。 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. PyAMF寻找一个特殊的类属性__amf__
,它用于为该类型的实例自定义编码和解码过程。 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. 设置静态属性将确保每个Question
实例都具有answers
属性集(通过getattr
设置),这将转而用于所需的数据存储区查找。
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. 重要的是不要让此设置适用于整个应用程序,因此任何问题都将具有出现在实例上的“答案”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.