Created
August 22, 2011 15:00
-
-
Save felipe-prenholato/1162570 to your computer and use it in GitHub Desktop.
This file contains 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
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