#24218: Use sub-query in ORM when distinct and order_by columns do not match
-------------------------------------+-------------------------------------
Reporter: Miroslav | Owner: nobody
Shubernetskiy |
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery distinct | Triage Stage: Accepted
order_by |
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Giovanni Totaro - aka Vanni):
**Workaround with
[https://docs.djangoproject.com/en/dev/ref/models/expressions/#subquery-
expressions Subquery() expressions]** (Django >= 1.11):
The same result of the really nice proposal by Carl and Harro:
{{{ Model.objects.all().distinct('foo',
order_by=('-baz',)).order_by('bar') }}}
can be currently obtained with something like this:
{{{
from django.db.models import Subquery
Model.objects.filter(
pk__in=Subquery(
Model.objects.all().distinct('foo').order_by('-baz').values('pk')
)
).order_by('bar')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24218#comment:11>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.01e35b6ba47f8b112c5a8dd328e63f02%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.