Fixed Q-object list query issue
This commit is contained in:
parent
6896818bfd
commit
196606438c
@ -36,7 +36,9 @@ class Q(object):
|
|||||||
OR = '||'
|
OR = '||'
|
||||||
AND = '&&'
|
AND = '&&'
|
||||||
OPERATORS = {
|
OPERATORS = {
|
||||||
'eq': 'this.%(field)s == %(value)s',
|
'eq': ('((this.%(field)s instanceof Array) && '
|
||||||
|
' this.%(field)s.indexOf(%(value)s) != -1) ||'
|
||||||
|
' this.%(field)s == %(value)s'),
|
||||||
'ne': 'this.%(field)s != %(value)s',
|
'ne': 'this.%(field)s != %(value)s',
|
||||||
'gt': 'this.%(field)s > %(value)s',
|
'gt': 'this.%(field)s > %(value)s',
|
||||||
'gte': 'this.%(field)s >= %(value)s',
|
'gte': 'this.%(field)s >= %(value)s',
|
||||||
@ -62,7 +64,8 @@ class Q(object):
|
|||||||
if not other.query[0]:
|
if not other.query[0]:
|
||||||
return self
|
return self
|
||||||
if self.query[0]:
|
if self.query[0]:
|
||||||
obj.query = ['('] + copy.deepcopy(self.query) + [op] + copy.deepcopy(other.query) + [')']
|
obj.query = (['('] + copy.deepcopy(self.query) + [op] +
|
||||||
|
copy.deepcopy(other.query) + [')'])
|
||||||
else:
|
else:
|
||||||
obj.query = copy.deepcopy(other.query)
|
obj.query = copy.deepcopy(other.query)
|
||||||
return obj
|
return obj
|
||||||
@ -111,11 +114,13 @@ class Q(object):
|
|||||||
value, field_js = self._build_op_js(op, key, value, value_name)
|
value, field_js = self._build_op_js(op, key, value, value_name)
|
||||||
js_scope[value_name] = value
|
js_scope[value_name] = value
|
||||||
js.append(field_js)
|
js.append(field_js)
|
||||||
|
print ' && '.join(js)
|
||||||
return ' && '.join(js)
|
return ' && '.join(js)
|
||||||
|
|
||||||
def _build_op_js(self, op, key, value, value_name):
|
def _build_op_js(self, op, key, value, value_name):
|
||||||
"""Substitute the values in to the correct chunk of Javascript.
|
"""Substitute the values in to the correct chunk of Javascript.
|
||||||
"""
|
"""
|
||||||
|
print op, key, value, value_name
|
||||||
if isinstance(value, RE_TYPE):
|
if isinstance(value, RE_TYPE):
|
||||||
# Regexes are handled specially
|
# Regexes are handled specially
|
||||||
if op.strip('$') == 'ne':
|
if op.strip('$') == 'ne':
|
||||||
|
@ -503,6 +503,22 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
obj = self.Person.objects(Q(name__ne=re.compile('^Gui'))).first()
|
obj = self.Person.objects(Q(name__ne=re.compile('^Gui'))).first()
|
||||||
self.assertEqual(obj, None)
|
self.assertEqual(obj, None)
|
||||||
|
|
||||||
|
def test_q_lists(self):
|
||||||
|
"""Ensure that Q objects query ListFields correctly.
|
||||||
|
"""
|
||||||
|
class BlogPost(Document):
|
||||||
|
tags = ListField(StringField())
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
BlogPost(tags=['python', 'mongo']).save()
|
||||||
|
BlogPost(tags=['python']).save()
|
||||||
|
|
||||||
|
self.assertEqual(len(BlogPost.objects(Q(tags='mongo'))), 1)
|
||||||
|
self.assertEqual(len(BlogPost.objects(Q(tags='python'))), 2)
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
def test_exec_js_query(self):
|
def test_exec_js_query(self):
|
||||||
"""Ensure that queries are properly formed for use in exec_js.
|
"""Ensure that queries are properly formed for use in exec_js.
|
||||||
"""
|
"""
|
||||||
@ -1172,10 +1188,15 @@ class QTest(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
q = Q()
|
q = Q()
|
||||||
examples = [
|
examples = [
|
||||||
({'name': 'test'}, 'this.name == i0f0', {'i0f0': 'test'}),
|
|
||||||
|
({'name': 'test'}, ('((this.name instanceof Array) && '
|
||||||
|
'this.name.indexOf(i0f0) != -1) || this.name == i0f0'),
|
||||||
|
{'i0f0': 'test'}),
|
||||||
({'age': {'$gt': 18}}, 'this.age > i0f0o0', {'i0f0o0': 18}),
|
({'age': {'$gt': 18}}, 'this.age > i0f0o0', {'i0f0o0': 18}),
|
||||||
({'name': 'test', 'age': {'$gt': 18, '$lte': 65}},
|
({'name': 'test', 'age': {'$gt': 18, '$lte': 65}},
|
||||||
'this.age <= i0f0o0 && this.age > i0f0o1 && this.name == i0f1',
|
('this.age <= i0f0o0 && this.age > i0f0o1 && '
|
||||||
|
'((this.name instanceof Array) && '
|
||||||
|
'this.name.indexOf(i0f1) != -1) || this.name == i0f1'),
|
||||||
{'i0f0o0': 65, 'i0f0o1': 18, 'i0f1': 'test'}),
|
{'i0f0o0': 65, 'i0f0o1': 18, 'i0f1': 'test'}),
|
||||||
]
|
]
|
||||||
for item, js, scope in examples:
|
for item, js, scope in examples:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user