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.