elfs: (Default)
[personal profile] elfs

For multi-select boxes where you have a list of things you want to filter on, and you want the queryset returned to be an OR’d set.

    qset = Q(mode__exact=modes[0])
    if len(modes) > 1:
        for mode in modes[1:]:
           qset = qset | Q(mode__exact=mode)
    r = Responses.objects.filter(qset)

Now, I need to figure out how to do this as a list comprehension!

For extra coolness, you can see what SQL you’re generating:

    print r.query.as_sql()
This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com

Date: 2009-08-14 11:39 am (UTC)
From: [identity profile] tamino.livejournal.com
What about .filter(mode__in=modes) ?

Profile

elfs: (Default)
Elf Sternberg

March 2026

S M T W T F S
1234567
8910111213 14
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 22nd, 2026 03:12 pm
Powered by Dreamwidth Studios