On Mon, Jun 22, 2009 at 1:58 PM, Sean Brant <brant.s...@gmail.com> wrote:
> > This is best explained with an example. > > i have a model for Stats > > class Stat(models.Model): > key = models.CharField(max_length=50) > value = models.TextField() > pub_date = models.DateTimeField() > > so if i create a few stat objects lets say these. > > stat1 = Stat(key='total_books_sold', value=100, > pub_date=datetime.datetime.now()) > stat2 = Stat(key='total_books_returned', value=10, > pub_date=datetime.datetime.now()) > stat3 = Stat(key='avg_foos', value=2.6, pub_date=datetime.datetime.now > ()) > > i would like to be able to one query and get the latest stat per key > so the query should only return. Even if we insert stats every hour. > It should only return 2 (the latest unique for key). > > [<Stat: total_books_sold >, <Stat: total_books_returned>, <Stat: > avg_foos>] > > I hope that makes sense. I'll do multiple queries if need be. > > > > It sounds like all you're trying to do is order by the primary key and then do a limit, so it would be: Stat.objects.order_by('-id')[:3] Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---