Skip to content

Instantly share code, notes, and snippets.

@olivx
Last active September 22, 2018 00:11
Show Gist options
  • Save olivx/89f2f52a471c602a24d680937a655bea to your computer and use it in GitHub Desktop.
Save olivx/89f2f52a471c602a24d680937a655bea to your computer and use it in GitHub Desktop.
filter_applicants multiples elements separator per ', ' example with Q() object
def add_q_object(self, keyword, q_object, query_parm):
for key in keyword:
kwargs = {query_parm: key.strip()}
q_object.add(Q(**kwargs), q_object.OR)
def filter_applicants(self, _queryset=None):
my_applicants = _queryset
self.is_valid()
q_object_cpf = Q()
if self.cleaned_data['cpf_bar']:
cpf_bar = self.cleaned_data['cpf_bar']
self.add_q_object(cpf_bar.split(','), q_object_cpf,
'candidate__usercpf__cpf')
return my_applicants.filter((q_object_cpf)).distinct()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment