Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created July 27, 2016 09:41
Show Gist options
  • Save kezabelle/5749e598a91890d4483865e3810b786b to your computer and use it in GitHub Desktop.
Save kezabelle/5749e598a91890d4483865e3810b786b to your computer and use it in GitHub Desktop.
For bradfordli123 in #django IRC
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