In my django app I have 'Documents'. Each document has one or more 'Revisions' that are ordered by date created. I'd like a way to get the latest revision for every document. The best I have so far is the code below, but I'm thinking there must be a way to do this with less database queries?
def get_document_templates():
result = []
for d in Document.objects.filter(is_template=True).all():
result.append(d.documentrevision_set.latest())
return result
I've been investigating 'annotate' and 'aggregate' filters, but can't figure out how to do this more efficiently. I'd rather not get my hands dirty in raw SQL, as the database backend may be changing in the near future. Anyone have any ideas ?
thanks!!