Hi Florian ,
> A Blog has Comments and Trackbacks. Trackbacks should be shown like
> comments but they get a div class="Trackback" instead of
> class="Comment" in HTML.
>
> My idea was to join all Trackbacks and Comments and use a an
> additional attribute to tell them apart in HTML:
>
> def commentList(self):
> """ Returns a joined list of comments and trackbacks, ordered
> descending by date. """
> comments = self.comment_set.all()
> trackbacks = self.trackback_set.all()
> joined = (comments + trackbacks).sort(cmp = lambda x,y:
> cmp(x.creationDate, y.creationDate), reverse = true)
> for j in joined:
> if isinstance(j, Comment):
> j.type = "Comment"
> elif isinstance(j, Trackback):
> j.type = "Trackback"
> return j
>
> Ok, that does not work, because you can't concat to QuerySets like
> that. I think that this solution would not be optimal anyway.
It looks like you are using your own Comment and Trackback models. So,
you could have a property on each of those classes to return their
string type:
@property
def css_type(self):
return "Comment"
@property
def css_type(self):
return "Trackback"
That will save you on the "isinstance(j, Comment)" test because you
could just use the css_type in your template directly:
<div class="{{ o.css_type }}">
...
</div>
Where o is an item from the joined list.
Secondly, to join those two query sets, you could first turn them into
Python lists like so:
comments = list(self.comment_set.all())
trackbacks = list(self.trackback_set.all())
After that the concatenation and the subsequent sort will work.
Since the comments and trackbacks are per blog entry, I am assuming
you would not have hundreds of those per blog entry. So, this type of
in-memory sort might be OK in terms of resource usage and performance.
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---