On Wed, 2011-05-25 at 02:59 -0700, Michel30 wrote: > Hello all, > > I have the following model: > > class Documentrevision(models.Model): > docrevid = models.AutoField(primary_key=True, > db_column='DocRevID') > documentid = models.ForeignKey(Document, db_column='DocumentID') > submitterid = models.ForeignKey('Author', db_column='SubmitterID') > documenttitle = models.CharField(max_length=765, > db_column='DocumentTitle') > publicationinfo = models.TextField(db_column='PublicationInfo', > blank=True) > versionnumber = models.IntegerField(db_column='VersionNumber') > abstract = models.TextField(db_column='Abstract', blank=True) > revisiondate = models.DateTimeField(null=True, > db_column='RevisionDate', blank=True) > timestamp = models.DateTimeField(db_column='TimeStamp') > obsolete = models.IntegerField(null=True, db_column='Obsolete', > blank=True) > keywords = models.CharField(max_length=720, db_column='Keywords', > blank=True) > note = models.TextField(db_column='Note', blank=True) > demanaged = models.IntegerField(null=True, db_column='Demanaged', > blank=True) > > Now I want to retrieve all entries that match obsolete=0, then order > on revisiondate and docrevid and last get only the highest > versionnumber for each group of documentid. > > I got this far: > found_entries = > Documentrevision.objects.filter(obsolete=0).order_by('-revisiondate','- > docrevid') > > This gives me all versionnumbers though: I've been trying several ways > to slice, group , do for-loops and can't get it to work.. > Any ideas are greatly appreciated :-) > Thanks >
DocumentRevision.objects.values('documentid').annotate(Max('docrevid)').filter(...) Should do the trick. -- Jani Tiainen -- 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.