#21604: Embed raw queries as subqueries when used with an __in filter
-------------------------------------+-------------------------------------
     Reporter:  alex@…               |                    Owner:  William
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Aivars Kalvāns):

 Hi!

 Is this still a thing? I had a similar need recently and solved it by
 using RawSQL:

 {{{
 from django.db.models.expressions import RawSQL

 NamedCategory.objects.create(id=1, name="first")
 NamedCategory.objects.create(id=2, name="second")
 NamedCategory.objects.create(id=3, name="third")
 NamedCategory.objects.create(id=4, name="fourth")

 query = DumbCategory.objects.filter(
     id__in=RawSQL("SELECT id FROM queries_dumbcategory WHERE id >= %s",
 params=[3])
 )

 self.assertEqual(set(query.values_list("id", flat=True)), {3, 4})
 print(query.query)
 }}}

 this works as expected and prints out

 {{{
 SELECT "queries_dumbcategory"."id" FROM "queries_dumbcategory" WHERE
 "queries_dumbcategory"."id" IN (SELECT id FROM queries_dumbcategory WHERE
 id >= 3)
 }}}

 which confirms that the raw subquery was embedded.
 Is there something that would make it better or more useful by using
 RawQuerySet?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21604#comment:7>
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/0107018c4382c62c-f9980168-3393-4e57-af19-4c64026bd756-000000%40eu-central-1.amazonses.com.

Reply via email to