Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created August 22, 2011 15:00
Show Gist options
  • Save felipe-prenholato/1162570 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/1162570 to your computer and use it in GitHub Desktop.
def do_simple_search(model,fields,q,objects=None):
if not objects:
objects = model.objects.select_related()
def make_subquery(fields,value):
_q = Q()
for fname in fields:
_q |= Q(**{fname+'__icontains': value})
return _q
if q:
searches = q.replace(', ',',').replace('| ','|').split(',')
for search in searches:
if search:
sub_searches = search.split('|')
query = Q()
for s in sub_searches:
query |= make_subquery(fields,s)
objects = objects.filter(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment