If he already has a list of posts, and wants to get the author for each
post, then it would be different. Both of these solutions are based on the
parents' data models.
1 way would be to grab the post.author from each post as you loop through.
If you enjoy experiments in poor performance that would be my recommended
approach. ;)

Another way would be to loop through each post you already have and get the
key for the Author, then do a single db.get passing in those keys. This way
you get an author entity for each post in a single round trip.

eg.

posts = SELECT * FROM Post #lets just pretend this gets us the posts... I'm
lazy
author_keys = list(set(Post.author.get_value_for_datastore(p) for p in
posts)) #list(set(list)) eliminates duplicate author keys
authors = Author.get(author_keys)

you could then, if you wanted to, create an authors_dict where the
author.key() is the key, and the author itself is the value, and then
reference the dict instead of the post.author (thereby eliminating another
call to datastore)

authors_dict = dict((author.key(), author) for author in authors)
for post in posts:
    text = post.text
    author = authors_dict[Post.author.get_value_for_datastore(post)]
    author_name = '%s %s' % (author.first_name, author.last_name)

Of course, this is all assuming you loath going back to the datastore in a
loop.  I try to minimize my DS accesses as much as possible.  Note that I
haven't actually tested any of this. I'm more wanting to show a possible
pattern as opposed to a workable solution.

- ryan.


On Thu, Oct 15, 2009 at 3:45 PM, wings <[email protected]> wrote:

>
> Here's how I would do it:
>
> class Author(db.Model):
>  """ set the key_name = user_id  when you create an Author
> instance"""
>  first_name = db.StringProperty()
>  last_name = db.StringProperty()
>
> class Post(db.Model):
>  text = db.TextProperty()
>  date = db.DateTimeProperty(auto_now_add=True)
>  author = db.ReferenceProperty(Author, collection_name="posts")
>
> # to get the author:
> author = Author.get_by_key_name(users.get_current_user().user_id())
> # to get five of the author's posts in reverse chronological order
> (like a blog):
> posts = author.posts.order("-date").fetch(5)
>
> read more about the reference property here:
>
> http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html#References
>
> On Oct 15, 9:48 am, skywa1ker <[email protected]> wrote:
> > I want to have very simple data structure.
> >
> > Users with properties: firsnane, lastname and posts with properties:
> > author(user), text, creationdate. Like very simple guestbook.
> >
> > And i want to show all posts on a page ordered by creating date and
> > with author name near the each post.
> >
> > For example i get all posts ( gql SELECT * FROM Post ), and now i need
> > to get a author for each post?
> >
> > Is it any other way to get Author?
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to