Merge pull request #2331 from abarto/fix/clone-retain-read-preference-read-concern

Add read_concern to cloned properties. Add read_concern to aggregate().
This commit is contained in:
Bastien Gérard 2020-05-23 23:22:56 +02:00 committed by GitHub
commit 107a1c34c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -6,6 +6,7 @@ Changelog
Development
===========
- (Fill this out as you fix issues and develop your features).
- Fixed a bug that made the queryset drop the read_preference after clone().
Changes in 0.20.0
=================

View File

@ -789,6 +789,7 @@ class BaseQuerySet:
"_snapshot",
"_timeout",
"_read_preference",
"_read_concern",
"_iter",
"_scalar",
"_as_pymongo",
@ -1311,10 +1312,11 @@ class BaseQuerySet:
final_pipeline = initial_pipeline + user_pipeline
collection = self._collection
if self._read_preference is not None:
if self._read_preference is not None or self._read_concern is not None:
collection = self._collection.with_options(
read_preference=self._read_preference
read_preference=self._read_preference, read_concern=self._read_concern
)
return collection.aggregate(final_pipeline, cursor={}, **kwargs)
# JS functionality

View File

@ -4021,6 +4021,32 @@ class TestQueryset(unittest.TestCase):
Number.drop_collection()
def test_clone_retains_settings(self):
"""Ensure that cloning retains the read_preference and read_concern
"""
class Number(Document):
n = IntField()
Number.drop_collection()
qs = Number.objects
qs_clone = qs.clone()
assert qs._read_preference == qs_clone._read_preference
assert qs._read_concern == qs_clone._read_concern
qs = Number.objects.read_preference(ReadPreference.PRIMARY_PREFERRED)
qs_clone = qs.clone()
assert qs._read_preference == ReadPreference.PRIMARY_PREFERRED
assert qs._read_preference == qs_clone._read_preference
qs = Number.objects.read_concern({"level": "majority"})
qs_clone = qs.clone()
assert qs._read_concern.document == {"level": "majority"}
assert qs._read_concern == qs_clone._read_concern
Number.drop_collection()
def test_using(self):
"""Ensure that switching databases for a queryset is possible
"""