views:

178

answers:

1

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 ?

+1  A: 

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.

njoyce
Great, it works, but I get 'aswers' as Array type in Flex. Is there any method to get aswers as ArrayCollection. Should I implement __readamf__ method or maybe exists simpler solution ? If the only way is __readamf__ method how it should look ? Thx in advance
With versions of PyAMF <= 0.5.1 you have to do this manually or set pyamf.amf3.use_proxy_default = True. This has been fixed in [the upcoming release of] 0.6 .. you can do:class Question(db.Model): class __amf__: static = ('answers',) proxy = ('answers',)You *can* use IExternalizable but then you would need to handle the encoding/decoding of the entire Question instance on the Python *and* the ActionScript side. I would not recommend using this feature for this use-case.
njoyce
I tried PyAMF 0.6 from trunk but seems to be not ready yet. By setting DEFAULT_ENCODING=AMF3 and use_proxy_default=True I managed to get things working, so I really appreciate your suggestions. The last question: It is possible to mass assign such as 'question.answers = answers' ? (i tried it in java and works but what about python/pyamf ?