Django 在原始 SQL 查询中放置 WHERE

Django placing of WHERE in raw SQL query

这个查询中WHERE的顺序是错误的,但现在是正确的。

postModel = list(PostModel.objects.raw(
SELECT max(pub_date), 
   count(topic_id) AS freq, 
   count(DISTINCT author) AS contributors 
   FROM crudapp_postmodel 
   WHERE author = "art" 
   GROUP BY topic_id 
   ORDER BY pub_date DESC
))

谢谢

简单:

SELECT max(pub_date), 
       count(topic_id) AS freq, 
       count(DISTINCT author) AS contributors 
       FROM crudapp_postmodel 
       WHERE author = "art" 
       GROUP BY topic_id 
       ORDER BY pub_date DESC