#29527: Multi-column comparisons
-------------------------------------+-------------------------------------
     Reporter:  Ryan Hiebert         |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  2.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  QuerySet.extra       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 > With one caveat, I needed to add the LHS as a calculated column using
 annotate, which is a bit of a kludge, which ends up returning annotated
 data that I didn't really need.

 FWIW this can now be avoided in two ways
 1. Use `QuerySet.alias` instead of `.annotate`
 2. Pass lookups directly to `filter`

 {{{#!python
 from django.db.models.lookups import GreaterThan
 from django.db.models import Func, F, Subquery

 class Row(Func):
     function = 'ROW'

 def after(queryset, value):
     return (
         queryset
         .filter(GreaterThan(
             Row('a', 'b', 'c', 'id'),
             Subquery(queryset.filter(id=value).values('a', 'b', 'c',
 'id'))
         ))
         .order_by('a', 'b', 'c', 'id')
     )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29527#comment:5>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.b8268ef72985bd29c71a5c96a61c587c%40djangoproject.com.

Reply via email to