#35146: Add support for annotate after union
-------------------------------------+-------------------------------------
     Reporter:  Alexandru Chirila    |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Alexandru Chirila):

 One workaround that exists for this is to build the query yourself and
 execute it as a raw SQL. However that's not really ideally:

 {{{#!python
 from django.db import connection

 query1 = Products1.objects.filter(foo="bar")
 query2 = Products2.objects.filter(foo="bar")
 union_query = query1.union(query2)

 compiler = union_query.query.get_compiler(connection=connection)
 sql, params = union_query.query.as_sql(compiler, connection)

 with connection.cursor() as cursor:
     cursor.execute(
         f"SELECT release_date, MIN(price), MAX(price) "
         f"FROM ({sql}) AS tmp_union "
         f"GROUP BY release_date",
         params,
     )

     for release_date, min_price, max_price in cursor.fetchall():
         ...
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35146#comment:1>
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/0107018d4639148a-f77fb789-b4f9-47a9-b5e0-95bf30c5f859-000000%40eu-central-1.amazonses.com.

Reply via email to