I’m trying to combine two Google App Engine datastore results. Because I then want to list them together in the template.
For example:
other_user_answers = UserAnswer.gql(“WHERE user = :1”, user.key()).fetch(500)
my_answers = UserAnswer.gql(“WHERE user = :1”, me.key()).fetch(500)
I want to combine these results based on question.key().id(). So that I can do something like this in the template:
{% for other_user_answer in other_user_answers %}
Question #{{other_user_answer.question.key.id}}<br>
User: {{other_user_answer.answer}}
Me: {{other_user_answer.my_answer}}
{% endfor %}
I’m not sure if I’m explaining it the best way or not! But something along those lines.
Also, I’m not quite sure what the Google App Engine datastore results come back in (like list, dict, etc), so I’m not quite sure what I’m even working with.
EDIT for more explanation:
Users are going to answer a bunch of questions. Then when I’m logged into the site and go to a different user’s page (Jimmy). I want to list all of Jimmy’s answers with all of my answers next to his as well. So I pull from the table UserAnswer and get all the answers for me…. and then for Jimmy. So now I have two sets of results. I want to spit them out as one though based on the Question ID.
Like:
Favorite color?
Me: red
Jimmy: blue
Favorite Ice cream?
Me: Chocolate
Jimmy: Vanilla
The best solution is probably to turn the list returned by one of the fetch() operations into a dict keyed by question id. Then you walk over the other list and pull the value out of the dict based on the question id, making sure to deal with missing entries (e.g. questions Jimmy answered but you didn’t), and store those on an extra attribute (e.g. my_answer). After that you can pass that list to the template.