Created
July 27, 2016 09:41
-
-
Save kezabelle/5749e598a91890d4483865e3810b786b to your computer and use it in GitHub Desktop.
For bradfordli123 in #django IRC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
teammembers = request.GET.getlist("teammembers") | |
all_qs = [] | |
for teammate in teammembers: | |
name = teammate.strip().split() | |
# nb: assuming only one space ("forename surname") isn't great. | |
try: | |
first_name = name[0].strip() | |
except IndexError: | |
# missing any input, so nothing to do... | |
continue | |
try: | |
last_name = name[1].strip() | |
except IndexError: | |
last_name = None | |
if first_name and last_name is not None: | |
# (first_name=x and last_name=y) | |
this_q = Q(first_name=first_name) & Q(last_name=last_name) | |
all_qs.append(this_q) | |
elif first_name: | |
# no surname entered I guess... (first_name=x) | |
all_qs.append(Q(first_name=first_name)) | |
# essentially "(first_name=x and last_name=y) OR (first_name=z and last_name=a)" | |
all_qs_combined = reduce(operator.or_, all_qs) | |
objs = TeamMember.objects.filter(all_qs_combined) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment