Hi fellow users,
I came across a situation where I needed to annotate a union-all query.
Django's ORM is so powerful, and it has those features separately, but it
cannot seem to handle annotating a union-all query.
I was able to solve my problem by using two views inside PostgreSQL. I created
> Then you'll need to use SQL. There's no way to do UNION in Django's
> ORM.
>
> An alternative is just to concatenate the two querysets:
> union = list(x) + list(y)
Or if the results are large and you don't want to consume double
the memory, you can use itertools.chain()
from itertools imp
On Sep 17, 5:05 pm, blumenkraft wrote:
> Did you mean or(|) instead of and(&)?
Yes, sorry.
> I need SQL UNION for efficiency reasons (using UNION instead of SQL
> generated by
> models.Advertisement.objects.filter( Q(company__name__search="test") |
> Q(contact__phone__istartswith="8495") is 60x
Did you mean or(|) instead of and(&)?
I need SQL UNION for efficiency reasons (using UNION instead of SQL
generated by
models.Advertisement.objects.filter( Q(company__name__search="test") |
Q(contact__phone__istartswith="8495") is 60x faster in my case)
On Sep 17, 7:51 pm, Daniel Roseman wrote:
On Sep 17, 3:39 pm, blumenkraft wrote:
> Hi,
>
> I have two querysets:
> x = models.Advertisement.objects.filter(company__name__search =
> "test")
> y = models.Advertisement.objects.filter(contact__phone__istartswith =
> "8495")
>
> Is there any simple way to get union (using SQL UNION clause) of
Hi,
I have two querysets:
x = models.Advertisement.objects.filter(company__name__search =
"test")
y = models.Advertisement.objects.filter(contact__phone__istartswith =
"8495")
Is there any simple way to get union (using SQL UNION clause) of these
querysets?
--~--~-~--~~~-
6 matches
Mail list logo