Skip to content

Commit 54975de

Browse files
committed
fix read_preference for PyMongo 3+
1 parent a7aead5 commit 54975de

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

mongoengine/queryset/base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,16 @@ def _cursor_args(self):
14441444
def _cursor(self):
14451445
if self._cursor_obj is None:
14461446

1447-
self._cursor_obj = self._collection.find(self._query,
1448-
**self._cursor_args)
1447+
# In PyMongo 3+, we define the read preference on a collection
1448+
# level, not a cursor level. Thus, we need to get a cloned
1449+
# collection object using `with_options` first.
1450+
if IS_PYMONGO_3 and self._read_preference is not None:
1451+
self._cursor_obj = self._collection\
1452+
.with_options(read_preference=self._read_preference)\
1453+
.find(self._query, **self._cursor_args)
1454+
else:
1455+
self._cursor_obj = self._collection.find(self._query,
1456+
**self._cursor_args)
14491457
# Apply where clauses to cursor
14501458
if self._where_clause:
14511459
where_clause = self._sub_js_fields(self._where_clause)

0 commit comments

Comments
 (0)