union of two QuerySets

1,069 views
Skip to first unread message

Rares Vernica

unread,
Dec 10, 2006, 10:37:25 PM12/10/06
to django...@googlegroups.com
Hi,

What is a way to get the union of two QuerySets?

Something like:

In [6]: a = Person.objects.filter(first_name__startswith='mic')

In [7]: b = Person.objects.filter(first_name__startswith='joh')

In [8]: a + b

Thanks a lot,
Ray

Jacob Kaplan-Moss

unread,
Dec 11, 2006, 12:22:48 AM12/11/06
to django...@googlegroups.com
On 12/10/06 9:37 PM, Rares Vernica wrote:
> What is a way to get the union of two QuerySets?

See
http://www.djangoproject.com/documentation/db_api/#complex-lookups-with-q-objects.

> In [6]: a = Person.objects.filter(first_name__startswith='mic')
>
> In [7]: b = Person.objects.filter(first_name__startswith='joh')

Try::

>>> Person.objects.filter(
... Q(first_name__startswith="mic") |
... Q(first_name__startswit="joh")
... )

Jacob

Rares Vernica

unread,
Dec 11, 2006, 5:08:23 PM12/11/06
to django...@googlegroups.com
Hi,

I know the Q way, but actually the filter contains already a lot of Qs.

I am looking for a way to combine "a" and "b" without going into their
filters.

Thanks

Jeremy Dunck

unread,
Dec 11, 2006, 5:23:43 PM12/11/06
to django...@googlegroups.com
On 12/11/06, Rares Vernica <rver...@gmail.com> wrote:
>
> Hi,
>
> I know the Q way, but actually the filter contains already a lot of Qs.
>
> I am looking for a way to combine "a" and "b" without going into their
> filters.

QuerySets are lazy. There's no downside to combining two
arbitrarily-complex querysets either before or after the filter
call(s).

Honza Král

unread,
Dec 12, 2006, 5:56:31 PM12/12/06
to django...@googlegroups.com
if you want a true union, use chain from itertools on the two querysets...


--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Àlex Pérez

unread,
Aug 1, 2012, 11:28:49 AM8/1/12
to django...@googlegroups.com
Hi,

it's better  Person.objects.filter(models.Q(first_name__startswith='mic'), models.Q(first_name__startswith='joh'))
(only one query...)


2012/8/1 Robin Pedersen <robin...@gmail.com>

Try:

a | b

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ie69j4ewJNUJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Alex Perez
alex....@bebabum.com
 
 bebabum be successful

c/ Còrsega 301-303, Àtic 2
08008 Barcelona
http://www.bebabum.com
http://www.facebook.com/bebabum
http://twitter.com/bebabum

This message is intended exclusively for its addressee and may contain
information that is confidential and protected by professional privilege. 
If you are not the intended recipient you are hereby notified that any 
dissemination, copy or disclosure of this communication is strictly prohibited by law.

Este mensaje se dirige exclusivamente a su destinatario y puede contener
información privilegiada o confidencial. Si no es vd. el destinatario indicado,
queda notificado que la utilización, divulgación y/o copia sin autorización 
está prohibida en virtud de la legislación vigente.

Le informamos que los datos personales que facilite/ha facilitado pasarán a
formar parte de un fichero responsabilidad de bebabum, S.L. y que tiene 
por finalidad gestionar las relaciones con usted. 
Tiene derecho al acceso, rectificación cancelación y oposición en nuestra
oficina ubicada en c/ Còrsega 301-303, Àtic 2 de Barcelona o a la dirección de e-mail lo...@bebabum.com

Tim Chase

unread,
Aug 1, 2012, 12:15:48 PM8/1/12
to django...@googlegroups.com, Àlex Pérez
On 08/01/12 10:28, �lex P�rez wrote:
> Hi,
>
> it's better Person.objects.filter(models.Q(first_**name__startswith='mic'),
> models.Q(first_**name__startswith='joh'))
> (only one query...)

I'm pretty sure this will get you the intersection (it uses AND)
rather than the union (which would be using OR). So I think you want

from django.db.models import Q
Person.objects.filter(
Q(first_name__startswith="mic") |
Q(first_name__startswith="joh")
)

using the "|" (OR) operator to join the two Q objects.

-tkc


For reference, you can read at

https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.filter

https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects


akaariai

unread,
Aug 1, 2012, 2:20:50 PM8/1/12
to Django users
On 1 elo, 19:15, Tim Chase <django.us...@tim.thechases.com> wrote:
> On 08/01/12 10:28, lex P rez wrote:
>
> > Hi,
>
> > it's better  Person.objects.filter(models.Q(first_**name__startswith='mic'),
> > models.Q(first_**name__startswith='joh'))
> > (only one query...)
>
> I'm pretty sure this will get you the intersection (it uses AND)
> rather than the union (which would be using OR).  So I think you want
>
>  from django.db.models import Q
>  Person.objects.filter(
>    Q(first_name__startswith="mic") |
>    Q(first_name__startswith="joh")
>    )
>
> using the "|" (OR) operator to join the two Q objects.
>
> -tkc
>
> For reference, you can read at
>
> https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db...
>
> https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-look...

As stated earlier the correct way to combine two existing querysets is
to do: combined = qs1 | qs2.

There are some limitations to what kind of querysets will work, but
for plain querysets which have just filers applied things should just
work.

- Anssi
Reply all
Reply to author
Forward
Message has been deleted
0 new messages