Is there a good reason why Django querysets can't be intersected?

32 views
Skip to first unread message

cool-RR

unread,
Mar 16, 2014, 7:58:36 PM3/16/14
to django...@googlegroups.com
Hi everyone,

I understand that there's no way to get the intersection of two querysets in Django. Whenever I need to get the intersection of two querysets, I need to refactor my code to get pure Qs for those querysets and intersect those. This can be annoying sometimes.

My question: Is there a good reason why Django querysets can't be intersected? Do you think it should be implemented?


Thanks,
Ram.

Shawn Milochik

unread,
Mar 16, 2014, 8:29:14 PM3/16/14
to django...@googlegroups.com
It seems to work for me:

a = Client.objects.filter(name__istartswith='a')                   b = Client.objects.filter(name__iendswith='t')                                  
print a.count()                                                   print b.count()                                                                                                                 
c = a & b                                                         print c.count() 

Ram Rachum

unread,
Mar 16, 2014, 8:33:46 PM3/16/14
to django-users
Oh my fucking God I'm an idiot. I distinctly remember it not working. Happy it's working! 

Is it implemented efficiently?


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SHT4Fka0uwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOzwKwE8d%3DJNaP%3DLzsCwYQOvpkGOvumU0mCNv7sOZ0jKhyvw8Q%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Ram Rachum

unread,
Mar 16, 2014, 8:34:25 PM3/16/14
to django-users
Also, is it a recently added feature?

Shawn Milochik

unread,
Mar 16, 2014, 9:11:22 PM3/16/14
to django...@googlegroups.com
I don't know anything about efficiency, but it works at least as far back as Django 1.3. I'd assume that, with lazy evaluation, it's probably about as efficient as anything in the ORM.

James Bennett

unread,
Mar 16, 2014, 9:17:30 PM3/16/14
to django...@googlegroups.com
QuerySets have supported the '&' operator (as measured by implementing the __and__() method) for as long as they've been in Django; the implementation dates all the way back to 0.95.

Ram Rachum

unread,
Mar 17, 2014, 7:22:57 AM3/17/14
to django-users
Wonderful!

Followup question: Is there any reason why ~ isn't supported? (It's supported on Q, but not QuerySet.)


On Mon, Mar 17, 2014 at 3:17 AM, James Bennett <ubern...@gmail.com> wrote:
QuerySets have supported the '&' operator (as measured by implementing the __and__() method) for as long as they've been in Django; the implementation dates all the way back to 0.95.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SHT4Fka0uwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages