擅长:python、mysql、java
<p>您需要使用<a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none" rel="nofollow noreferrer">extra</a>执行一些本机SQL操作。在</p>
<p>(非常粗略地)</p>
<pre>
<code>
Lesson.objects.extra(select={'visit_count': "SELECT COUNT(*) FROM statlesson WHERE statlesson.lesson_id=lesson.id"}).order_by('visit_count')
</code>
</pre>
<p>您需要确保<code>SELECT COUNT(*) FROM statlesson WHERE statlesson.lesson_id=lesson.id</code>具有项目的write db表名(因为statlesson和lesson不正确)。在</p>