Q:

Django query that requires grouping and aggregating

I'm trying to do this query in django and I'm not sure what the best way to do it is. I neeed to group and then count and order by that count...
I have a through model like:
ProjectView(models.Model):
  user = models.ForeignKey(User)
  project = models.ForeignKey(Project)
  datetime_created = models.DateTimeField()

I want to get a list of projects ordered by most views since yesterday. 

A:

Something like this (not tested):
projects = Project.objects \
                  .annotate(total_views=Count('projectview__project'))
                  .order_by('-total_views')

Where Project is the model name and projectview is the through table.

