Cecil Westerhof schrieb:
> In model I have:
> class BlogComment(SQLObject):
> date = DateTimeCol(default = datetime.now)
> commentText = StringCol()
> commenter = ForeignKey('User', cascade = True)
> entry = ForeignKey('BlogEntry', cascade = True)
>
> To receive the comments on a certain blog entry, I have defined in my
> controller:
> @expose()
> def getComments(self, entryId):
> comments = list()
> for comment in BlogComment.select(BlogComment.q.entryID == entryId,
> orderBy =
> BlogComment.q.date).reversed():
> comments.append(dict(date = comment.date,
> commenter = comment.commenter.display_name,
> commentText = comment.commentText))
> return dict(comments = comments)
>
> Is this the good way to receive this information, or can it be done more
> efficient?
It depends on when you want the actual SQL query execution to happen: in
your controller code, or when the template is rendered.
You are doing the former, because when you iterate of the SelectResult
in the for loop, the BlogComment instances are retrieved. Alternatively,
you can just pass the SelectResult to the template:
@expose('.templates.foo')
def comments(self, entry_id):
comments = BlogComment.select(BlogComment.q.entryID == entry_id,
orderBy = BlogComment.q.date).reversed()
return dict(comments = comments)
<!-- foo.kid -->
...
<div py:for="comment in comments">
<div class="comment">
Posted by
<span class="comment-author"
py:content="comment.commenter.display_name" /> on
<span class="comment-date py:content="comment.date" />
<!-- etc. -->
</div>
</div>
<!-- end foo.kid -->
For me, it's a matter of taste. It might be easier to measure
performance and to debug when you make sure all SQL queries get executed
within the controller method by flattening all your model objects in the
controller method. But in TurboGears the distinction between what's
happening in the controller and what in the view/template is hard to
make, because the template rendering is done through the expose
decorator, which wraps the controller method.
Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---