Added slave_okay syntax to querysets.
* slave_okay (optional): if True, allows this query to be run against a replica secondary.
This commit is contained in:
parent
56f00a64d7
commit
55e20bda12
@ -336,6 +336,7 @@ class QuerySet(object):
|
|||||||
self._snapshot = False
|
self._snapshot = False
|
||||||
self._timeout = True
|
self._timeout = True
|
||||||
self._class_check = True
|
self._class_check = True
|
||||||
|
self._slave_okay = False
|
||||||
|
|
||||||
# If inheritance is allowed, only return instances and instances of
|
# If inheritance is allowed, only return instances and instances of
|
||||||
# subclasses of the class being used
|
# subclasses of the class being used
|
||||||
@ -430,7 +431,7 @@ class QuerySet(object):
|
|||||||
|
|
||||||
return spec
|
return spec
|
||||||
|
|
||||||
def __call__(self, q_obj=None, class_check=True, **query):
|
def __call__(self, q_obj=None, class_check=True, slave_okay=False, **query):
|
||||||
"""Filter the selected documents by calling the
|
"""Filter the selected documents by calling the
|
||||||
:class:`~mongoengine.queryset.QuerySet` with a query.
|
:class:`~mongoengine.queryset.QuerySet` with a query.
|
||||||
|
|
||||||
@ -440,6 +441,8 @@ class QuerySet(object):
|
|||||||
objects, only the last one will be used
|
objects, only the last one will be used
|
||||||
:param class_check: If set to False bypass class name check when
|
:param class_check: If set to False bypass class name check when
|
||||||
querying collection
|
querying collection
|
||||||
|
:param slave_okay: if True, allows this query to be run against a
|
||||||
|
replica secondary.
|
||||||
:param query: Django-style query keyword arguments
|
:param query: Django-style query keyword arguments
|
||||||
"""
|
"""
|
||||||
query = Q(**query)
|
query = Q(**query)
|
||||||
@ -449,6 +452,7 @@ class QuerySet(object):
|
|||||||
self._mongo_query = None
|
self._mongo_query = None
|
||||||
self._cursor_obj = None
|
self._cursor_obj = None
|
||||||
self._class_check = class_check
|
self._class_check = class_check
|
||||||
|
self._slave_okay = slave_okay
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def filter(self, *q_objs, **query):
|
def filter(self, *q_objs, **query):
|
||||||
@ -506,6 +510,7 @@ class QuerySet(object):
|
|||||||
cursor_args = {
|
cursor_args = {
|
||||||
'snapshot': self._snapshot,
|
'snapshot': self._snapshot,
|
||||||
'timeout': self._timeout,
|
'timeout': self._timeout,
|
||||||
|
'slave_okay': self._slave_okay
|
||||||
}
|
}
|
||||||
if self._loaded_fields:
|
if self._loaded_fields:
|
||||||
cursor_args['fields'] = self._loaded_fields.as_dict()
|
cursor_args['fields'] = self._loaded_fields.as_dict()
|
||||||
|
@ -413,6 +413,19 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
obj_id = Blog.objects.insert(blog1, load_bulk=False)
|
obj_id = Blog.objects.insert(blog1, load_bulk=False)
|
||||||
self.assertEquals(obj_id.__class__.__name__, 'ObjectId')
|
self.assertEquals(obj_id.__class__.__name__, 'ObjectId')
|
||||||
|
|
||||||
|
def test_slave_okay(self):
|
||||||
|
"""Ensures that a query can take slave_okay syntax
|
||||||
|
"""
|
||||||
|
person1 = self.Person(name="User A", age=20)
|
||||||
|
person1.save()
|
||||||
|
person2 = self.Person(name="User B", age=30)
|
||||||
|
person2.save()
|
||||||
|
|
||||||
|
# Retrieve the first person from the database
|
||||||
|
person = self.Person.objects(slave_okay=True).first()
|
||||||
|
self.assertTrue(isinstance(person, self.Person))
|
||||||
|
self.assertEqual(person.name, "User A")
|
||||||
|
self.assertEqual(person.age, 20)
|
||||||
|
|
||||||
def test_repeated_iteration(self):
|
def test_repeated_iteration(self):
|
||||||
"""Ensure that QuerySet rewinds itself one iteration finishes.
|
"""Ensure that QuerySet rewinds itself one iteration finishes.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user