如何使用Django执行SQL LEFT JOIN?

Ty.*_*Ty. 2 database django postgresql performance

基本上我需要每个条目评论的计数:

SELECT e.*, COUNT(c.id) as comments FROM blog_entry e LEFT JOIN blog_comment c ON e.id = c.entry_id GROUP BY e.id, e.name, e.name_slug, e.date_published, e.category, e.image, e.body, e.is_published, e.views, e.subscription_sent ORDER BY e.date_published DESC LIMIT 15;
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用Django来解决这个问题.

这是我到目前为止,它完美的工作,除了没有评论计数.有人能指出我正确的方向使用Django进行这样的连接吗?

from project.blog.models import Entry, Comment

def index(request):
    latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:15]
    return render_to_response('blog/index.html', {'latest_entry_list': latest_entry_list)
Run Code Online (Sandbox Code Playgroud)

Tia*_*ago 6

django 1.1支持聚合查询,你可以通过svn trunk获取最后一个版本.该文档已更新

http://docs.djangoproject.com/en/dev/topics/db/aggregation/