On Monday 25 Jan 2010 3:25:51 pm Tomasz Zieliński wrote: > > I have a model 'Player' and a Model 'Handicap'. Handicap will have > > several fields, one of which is a foreign key to player. Now I need to > > get the Handicap instance when the player had the highest handicap. I can > > use Max to find the highest handicap and do a query to identify the > > instance where the handicap is at Max value. Is there a simpler way of > > doing this? > > I'm not sure if I understand correctly, but you are probably asking > for something like this: > > http://groups.google.pl/group/django-users/browse_thread/thread/3b3cd4cbad4 > 78d34/3e4c87f336696054?hl=pl&q= > http://stackoverflow.com/questions/2074514/django-query-that-get-most-rece > nt-objects-from-different-categories/2076665 >
maybe I was obscure. Here is the code I currently have: def latesthandicap(self): if self.handicap_set.all().count()>0: latestdate = self.handicap_set.all().aggregate(Max('valto')) return Handicap.objects.get(player=self,valto=latestdate['valto__max']) else: return 0 which is two queries. But anyway your references show that it cannot be done in one query. -- regards Kenneth Gonsalves Senior Project Officer NRC-FOSS http://nrcfosshelpline.in/web/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.