If we want show last commented posts:
Add to settings.py
ADVANCED_COMMENTS_COUNT=True
Add to apps/blog/templatetags/sidebar.py
def sidebar(context):
if settings.ADVANCED_COMMENTS_COUNT:
cursor = connection.cursor()
cursor.execute("select max(comment_nodes.id) from comment_nodes where approved = 1 group by comment_nodes.object_id order by comment_nodes.pub_date desc limit 5")
comments=CommentNode.objects.select_related().extra(where=['comment_nodes.id in (%s)'%(','.join([str(m[0]) for m in cursor.fetchall()]))])
else:
comments = CommentNode.objects.filter(approved=True).select_related().order_by('-pub_date')[:5]
last_posts = Post.objects.all().order_by('-date')[:5]
# TODO: return all variables from RequestContext dictionary
return {'STATIC_URL': context['STATIC_URL'], 'request': context['request'], 'last_posts': last_posts, 'comments': comments}