Adding QuerySet(read_preference=pymongo.ReadPreference.X) and QuerySet().read_preference() method to override connection-level read_preference on a per-query basis.
This commit is contained in:
parent
f0f1308465
commit
f2049e9c18
@ -53,6 +53,7 @@ class QuerySet(object):
|
|||||||
self._timeout = True
|
self._timeout = True
|
||||||
self._class_check = True
|
self._class_check = True
|
||||||
self._slave_okay = False
|
self._slave_okay = False
|
||||||
|
self._read_preference = None
|
||||||
self._iter = False
|
self._iter = False
|
||||||
self._scalar = []
|
self._scalar = []
|
||||||
|
|
||||||
@ -75,7 +76,8 @@ class QuerySet(object):
|
|||||||
|
|
||||||
copy_props = ('_initial_query', '_query_obj', '_where_clause',
|
copy_props = ('_initial_query', '_query_obj', '_where_clause',
|
||||||
'_loaded_fields', '_ordering', '_snapshot',
|
'_loaded_fields', '_ordering', '_snapshot',
|
||||||
'_timeout', '_limit', '_skip', '_slave_okay', '_hint')
|
'_timeout', '_limit', '_skip', '_slave_okay', '_hint',
|
||||||
|
'_read_preference')
|
||||||
|
|
||||||
for prop in copy_props:
|
for prop in copy_props:
|
||||||
val = getattr(self, prop)
|
val = getattr(self, prop)
|
||||||
@ -109,7 +111,8 @@ class QuerySet(object):
|
|||||||
self._collection.ensure_index(fields, **index_spec)
|
self._collection.ensure_index(fields, **index_spec)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __call__(self, q_obj=None, class_check=True, slave_okay=False, **query):
|
def __call__(self, q_obj=None, class_check=True, slave_okay=False, read_preference=None,
|
||||||
|
**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.
|
||||||
|
|
||||||
@ -121,6 +124,8 @@ class QuerySet(object):
|
|||||||
querying collection
|
querying collection
|
||||||
:param slave_okay: if True, allows this query to be run against a
|
:param slave_okay: if True, allows this query to be run against a
|
||||||
replica secondary.
|
replica secondary.
|
||||||
|
:params read_preference: if set, overrides connection-level
|
||||||
|
read_preference from `ReplicaSetConnection`.
|
||||||
:param query: Django-style query keyword arguments
|
:param query: Django-style query keyword arguments
|
||||||
"""
|
"""
|
||||||
query = Q(**query)
|
query = Q(**query)
|
||||||
@ -129,6 +134,8 @@ class QuerySet(object):
|
|||||||
self._query_obj &= query
|
self._query_obj &= query
|
||||||
self._mongo_query = None
|
self._mongo_query = None
|
||||||
self._cursor_obj = None
|
self._cursor_obj = None
|
||||||
|
if read_preference is not None:
|
||||||
|
self._read_preference = read_preference
|
||||||
self._class_check = class_check
|
self._class_check = class_check
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -229,8 +236,10 @@ class QuerySet(object):
|
|||||||
cursor_args = {
|
cursor_args = {
|
||||||
'snapshot': self._snapshot,
|
'snapshot': self._snapshot,
|
||||||
'timeout': self._timeout,
|
'timeout': self._timeout,
|
||||||
'slave_okay': self._slave_okay
|
'slave_okay': self._slave_okay,
|
||||||
}
|
}
|
||||||
|
if self._read_preference is not None:
|
||||||
|
cursor_args['read_preference'] = self._read_preference
|
||||||
if self._loaded_fields:
|
if self._loaded_fields:
|
||||||
cursor_args['fields'] = self._loaded_fields.as_dict()
|
cursor_args['fields'] = self._loaded_fields.as_dict()
|
||||||
return cursor_args
|
return cursor_args
|
||||||
@ -802,6 +811,15 @@ class QuerySet(object):
|
|||||||
self._slave_okay = enabled
|
self._slave_okay = enabled
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def read_preference(self, read_preference):
|
||||||
|
"""Change the read_preference when querying.
|
||||||
|
|
||||||
|
:param read_preference: override ReplicaSetConnection-level
|
||||||
|
preference.
|
||||||
|
"""
|
||||||
|
self._read_preference = read_preference
|
||||||
|
return self
|
||||||
|
|
||||||
def delete(self, safe=False):
|
def delete(self, safe=False):
|
||||||
"""Delete the documents matched by the query.
|
"""Delete the documents matched by the query.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user