How to get latest of many groups at once

I have a model like this:

class Foo(models.Model):
    date = models.DateTimeField()
    language = models.TextField()
    # other stuff

And I want to group the Foos by language, then get the latest one in each group. I haven't been able to figure out how to use django's QuerySet API to do this (honestly, I don't know how to do it in SQL either). So for example:

pk | date   |    language
---+--------+------------------
1  | 1:00   |    python
2  | 1:30   |    python/django
3  | 1:45   |    haskell
4  | 2:15   |    python
5  | 2:45   |    haskell

I want to get something resembling this result:

{ 'python': 4, 'python/django': 2, 'haskell': 5 }

Where perhaps instead of numbers those are complete Foo objects.

8
задан luqui 23 March 2011 в 02:15
поделиться