Fixed Q object ObjectId comparison issue
This commit is contained in:
parent
0b1c506626
commit
6e77e32855
@ -96,14 +96,14 @@ class Q(object):
|
|||||||
# Create a custom variable name for this operator
|
# Create a custom variable name for this operator
|
||||||
op_value_name = '%so%s' % (value_name, j)
|
op_value_name = '%so%s' % (value_name, j)
|
||||||
# Construct the JS that uses this op
|
# Construct the JS that uses this op
|
||||||
operation_js = self._build_op_js(op, key, value,
|
value, operation_js = self._build_op_js(op, key, value,
|
||||||
op_value_name)
|
op_value_name)
|
||||||
# Update the js scope with the value for this op
|
# Update the js scope with the value for this op
|
||||||
js_scope[op_value_name] = value
|
js_scope[op_value_name] = value
|
||||||
js.append(operation_js)
|
js.append(operation_js)
|
||||||
else:
|
else:
|
||||||
# Construct the JS for this field
|
# Construct the JS for this field
|
||||||
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)
|
||||||
return ' && '.join(js)
|
return ' && '.join(js)
|
||||||
@ -119,12 +119,17 @@ class Q(object):
|
|||||||
op_js = Q.OPERATORS['regex_eq']
|
op_js = Q.OPERATORS['regex_eq']
|
||||||
else:
|
else:
|
||||||
op_js = Q.OPERATORS[op.strip('$')]
|
op_js = Q.OPERATORS[op.strip('$')]
|
||||||
|
|
||||||
|
# Comparing two ObjectIds in Javascript doesn't work..
|
||||||
|
if isinstance(value, pymongo.objectid.ObjectId):
|
||||||
|
value = str(value)
|
||||||
|
|
||||||
# Perform the substitution
|
# Perform the substitution
|
||||||
operation_js = op_js % {
|
operation_js = op_js % {
|
||||||
'field': key,
|
'field': key,
|
||||||
'value': value_name
|
'value': value_name
|
||||||
}
|
}
|
||||||
return operation_js
|
return value, operation_js
|
||||||
|
|
||||||
class QuerySet(object):
|
class QuerySet(object):
|
||||||
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
||||||
|
@ -376,6 +376,11 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
post6 = BlogPost(published=False)
|
post6 = BlogPost(published=False)
|
||||||
post6.save()
|
post6.save()
|
||||||
|
|
||||||
|
# Check ObjectId lookup works
|
||||||
|
obj = BlogPost.objects(id=post1.id).first()
|
||||||
|
self.assertEqual(obj, post1)
|
||||||
|
|
||||||
|
# Check Q object combination
|
||||||
date = datetime(2010, 1, 10)
|
date = datetime(2010, 1, 10)
|
||||||
q = BlogPost.objects(Q(publish_date__lte=date) | Q(published=True))
|
q = BlogPost.objects(Q(publish_date__lte=date) | Q(published=True))
|
||||||
posts = [post.id for post in q]
|
posts = [post.id for post in q]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user