title: How to execute a GROUP BY ... COUNT or SUM in Django ORM?
body: <p><strong>Prologue:</strong></p>
  
  <p>This is a question arising often in SO:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/5947475/django-models-group-by">Django Models Group By</a></li>
  <li><a href="https://stackoverflow.com/questions/327807/django-equivalent-for-count-and-group-by">Django equivalent for count and group by</a></li>
  <li><a href="https://stackoverflow.com/questions/629551/how-to-query-as-group-by-in-django">How to query as GROUP BY in django?</a></li>
  <li><a href="https://stackoverflow.com/questions/40175134/how-to-use-the-orm-for-the-equivalent-of-a-sql-count-group-and-join-query/45563598#45563598">How to use the ORM for the equivalent of a SQL count, group and join query?</a></li>
  </ul>
  
  <p>I have composed an example on SO Documentation but since the Documentation will get shut down on August 8, 2017, I will follow the suggestion of <a href="https://meta.stackoverflow.com/questions/354217/sunsetting-documentation/354302#354302">this widely upvoted and discussed meta answer</a> and transform my example to a self-answered post.</p>
  
  <p>Of course, I would be more than happy to see any different approach as well!!</p>
  
  <hr>
  
  <p><strong>Question:</strong></p>
  
  <p>Assume the model:</p>
  
  <pre><code>class Books(models.Model):
      title  = models.CharField()
      author = models.CharField()
      price = models.FloatField()
  </code></pre>
  
  <p>How can I perform the following queries on that model utilizing Django ORM:</p>
  
  <ul>
  <li><p><code>GROUP BY ... COUNT</code>:</p>
  
  <pre><code>SELECT author, COUNT(author) AS count
  FROM myapp_books GROUP BY author
  </code></pre></li>
  <li><p><code>GROUP BY ... SUM</code>:</p>
  
  <pre><code>SELECT author,  SUM (price) AS total_price
  FROM myapp_books GROUP BY author
  </code></pre></li>
  </ul>