title: Django aggregate count of records per day
body: <p>I've got a django app that is doing some logging. My model looks like this:</p>
  
  <pre><code>class MessageLog(models.Model):
      logtime = models.DateTimeField(auto_now_add=True)
      user = models.CharField(max_length=50)
      message = models.CharField(max_length=512)
  </code></pre>
  
  <p>What a want to do is get the average number of messages logged per day of the week so that I can see which days are the most active. I've managed to write a query that pulls the total number of messages per day which is:</p>
  
  <pre><code>for i in range(1, 8):
      MessageLog.objects.filter(logtime__week_day=i).count()
  </code></pre>
  
  <p>But I'm having trouble calculating the average in a query. What I have right now is:</p>
  
  <pre><code>for i in range(1, 8):
      MessageLog.objects.filter(logtime__week_day=i).annotate(num_msgs=Count('id')).aggregate(Avg('num_msgs'))
  </code></pre>
  
  <p>For some reason this is returning 1.0 for every day though. I looked at the SQL it is generating and it is:</p>
  
  <pre><code>SELECT AVG(num_msgs) FROM (
  SELECT 
  `myapp_messagelog`.`id` AS `id`, `myapp_messagelog`.`logtime` AS `logtime`, 
  `myapp_messagelog`.`user` AS `user`, `myapp_messagelog`.`message` AS `message`, 
  COUNT(`myapp_messagelog`.`id`) AS `num_msgs` 
  FROM `myapp_messagelog` 
  WHERE DAYOFWEEK(`myapp_messagelog`.`logtime`) = 1 
  GROUP BY `myapp_messagelog`.`id` ORDER BY NULL
  ) subquery
  </code></pre>
  
  <p>I think the problem might be coming from the GROUP BY id but I'm not really sure. Anyone have any ideas or suggestions? Thanks in advance!</p>