基本上我需要每個條目的評論計數:如何使用Django執行SQL LEFT JOIN?
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;
但我不知道如何去這個使用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)
因此,除了放入原始SQL之外,目前還沒有其他方式來處理此類問題? – 2009-01-16 15:48:51