Re: union of two QuerySets
There might be situations where you already have a couple of querysets, and want to combine them, or for DRYness it might be an idea to create functions that return querysets, and combine the results: combined = self.get_some() | self.get_others() And querysets are lazily executed, so unless you actually use the results of the individual querysets, only one combined query is executed, just like in your example. On Wednesday, August 1, 2012 5:28:49 PM UTC+2, À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...) > > > 2012/8/1 Robin Pedersen <robinpe...@gmail.com> > >> On Monday, December 11, 2006 4:37:25 AM UTC+1, Rares Vernica wrote: >>> >>> 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 >>> >> >> 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-users@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> > > > > -- > Alex Perez > alex.pe...@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 l...@bebabum.com > -- 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/-/Eye_qpp3XowJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: union of two QuerySets
On 1 elo, 19:15, Tim Chasewrote: > 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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: union of two QuerySets
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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: union of two QuerySets
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 <robinpe...@gmail.com> > On Monday, December 11, 2006 4:37:25 AM UTC+1, Rares Vernica wrote: >> >> 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 >> > > 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-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- Alex Perez alex.pe...@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 l...@bebabum.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: union of two QuerySets
On Monday, December 11, 2006 4:37:25 AM UTC+1, Rares Vernica wrote: > > 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 > 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-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Union of two querysets?
Yes you can do a union. Use the "|" (pipe) operator. union = queryset1 | queryset2 or you can replace queryset1 with the union as follows: queryset1 |= queryset2 Nick On 16/02/10 02:39, rebus_ wrote: On 16 February 2010 03:28, ydjangowrote: I have two query sets with two different where clauses on same table and same columns in select. Is it possible to have their union. I tried qryset = qryset1 + qryset2 It gave me - "+ unsupported operand" -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. Perhaps you could try to use OR lookup using Q objects? http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Union of two querysets?
On 16 February 2010 03:28, ydjangowrote: > I have two query sets with two different where clauses on same table > and same columns in select. Is it possible to have their union. > > I tried qryset = qryset1 + qryset2 > It gave me - "+ unsupported operand" > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > Perhaps you could try to use OR lookup using Q objects? http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Union of two querysets?
I have two query sets with two different where clauses on same table and same columns in select. Is it possible to have their union. I tried qryset = qryset1 + qryset2 It gave me - "+ unsupported operand" -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: union of two QuerySets
if you want a true union, use chain from itertools on the two querysets... On 12/11/06, Jeremy Dunck <[EMAIL PROTECTED]> wrote: > > On 12/11/06, Rares Vernica <[EMAIL PROTECTED]> 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 E-Mail: [EMAIL PROTECTED] ICQ#: 107471613 Phone: +420 606 678585 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: union of two QuerySets
On 12/11/06, Rares Vernica <[EMAIL PROTECTED]> 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). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: union of two QuerySets
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 Jacob Kaplan-Moss wrote: > 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 > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: union of two QuerySets
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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---