Django extra 和 annotate

>>> qs=Question.objects.extra(select={'anum': 'SELECT COUNT(*) FROM questions_answer WHERE questions_answer.question_id = questions_question.id'},).extra(order_by=['-anum'])
>>> [q.anum for q in qs]
[3, 3, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0]
>>> qs2=Question.objects.annotate(n=Count('answer')).order_by('-n')
>>> [q.n for q in qs2]
[3, 3, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0]
>>>

 

posted @ 2014-03-05 00:48  LisPythoniC  阅读(618)  评论(0编辑  收藏  举报