On Oct 19, 5:25 pm, Tomasz Zieliński
<[email protected]> wrote:
> On 19 Paź, 17:52, Daniel Roseman <[email protected]> wrote:
>
> > On Oct 19, 4:17 pm, Tomasz Zieliński
>
> > It's not clear what you mean by 'all latest ObjectRevisions'. Do you
> > mean all ObjectRevisions for a particular object? If so:
> > myobject.objectrevision_set.all()
>
> I was unclear, but SQL should explain what I want to do - select
> all ObjectRevision-s that are latest in Object-s that they belong to.
> Moreover, I want to do this in with one ORM query, without resorting
> to SQL or splitting the query too much.
>
You can't do this in one query with Django's ORM.
One way of doing it in two queries might be to get use .annotate() to
add the max id of the related objectrevision for each revision, then
get all those objectrevisions (untested):
objects = Object.objects.all().annotate(revision_id=Max
('objectrevision__id'))
objectrevisions = ObjectRevision.objects.filter(id__in=
[o.revision_id for o in objects])
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---