#34940: Filter queryset on "tuples" of values for multiple columns
-------------------------------------+-------------------------------------
     Reporter:  Xavier Blanchot      |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:  Filter               |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

 There is a way to achieve this, but I've only tested this with PostgreSQL
 and it **only** works with psycopg2 – psycopg (3) changes the way
 composite type adaptation works.  (what's more confusing is that if you
 try this with psycopg (3) it _mogrifies_ fine but if you try to run the
 query it fails.)

 {{{
 class Foo(Model):
     a = models.IntegerField()
     b = models.IntegerField()

 Foo.objects.create(a=1, b=1)
 Foo.objects.create(a=1, b=2)
 Foo.objects.create(a=1, b=3)

 qs = (
     Foo.objects.annotate(ab=RawSQL("a, b", params=[]))
     .filter(ab__in=[(1, 1), (1, 3)])
     .values("a", "b", "ab")
 )
 print(qs)
 }}}

 gives me:

 {{{
 <QuerySet [{'a': 1, 'b': 1, 'ab': '(1,1)'}, {'a': 1, 'b': 3, 'ab':
 '(1,3)'}]>
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34940#comment:3>
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/0107018b93d41941-a2a7ff50-04bf-465c-b7be-d943b77f5b66-000000%40eu-central-1.amazonses.com.

Reply via email to