<s> import shlex , operator <EOL> from django . db . models import Q <EOL> def query_to_q_object ( query , field ) : <EOL> conditions = [ ] <EOL> if isinstance ( query , unicode ) : <EOL> query = query . encode ( '<STR_LIT:utf8>' ) <EOL> terms = shlex . split ( query ) <EOL> for term in terms : <EOL> negate = False <EOL> if term . startswith ( '<STR_LIT:->' ) : <EOL> negate = True <EOL> term = term [ <NUM_LIT:1> : ] <EOL> q = Q ( ** { '<STR_LIT>' % field : term } ) <EOL> if negate : <EOL> q = ~ q <EOL> conditions . append ( q ) <EOL> return reduce ( operator . and_ , conditions ) </s>
