From 72ebaa52e9330a063d7d45a72ce65710498ef99f Mon Sep 17 00:00:00 2001 From: Tom Floyer Date: Sat, 8 Dec 2018 22:10:10 +0300 Subject: [PATCH 1/4] Ensure that QuerySet.only() works correctly after QuerySet.count() This test checks if .only() method of QuerySet instance works after using .count() method. --- tests/queryset/queryset.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index 9e251af7..b44c36df 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -4720,6 +4720,28 @@ class QuerySetTest(unittest.TestCase): 'password_salt').only('email').to_json() self.assertEqual('[{"email": "ross@example.com"}]', serialized_user) + def test_only_after_count(self): + """Test that only() works after count()""" + + class User(Document): + name = StringField() + age = IntField() + address = StringField() + User.drop_collection() + User(name="User", age=50, + address="Moscow, Russia").save() + + user_queryset = User.objects(age=50) + + result = user_queryset.only("name", "age").as_pymongo().first() + self.assertEqual(result, {"name": "User", "age": 50}) + + result = user_queryset.count() + self.assertEqual(result, 1) + + result = user_queryset.only("name", "age").as_pymongo().first() + self.assertEqual(result, {"name": "User", "age": 50}) + def test_no_dereference(self): class Organization(Document): From 3194a37fcb9654458373ffe0a738038003f7e521 Mon Sep 17 00:00:00 2001 From: Tom Floyer Date: Sat, 8 Dec 2018 22:14:43 +0300 Subject: [PATCH 2/4] Reset cursor object after .count() This change fixes incorrect result of .only() method of QuerySet instance after using .count(). --- mongoengine/queryset/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 6c36d984..862a1826 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -396,7 +396,9 @@ class BaseQuerySet(object): """ if self._limit == 0 and with_limit_and_skip is False or self._none: return 0 - return self._cursor.count(with_limit_and_skip=with_limit_and_skip) + count = self._cursor.count(with_limit_and_skip=with_limit_and_skip) + self._cursor_obj = None + return count def delete(self, write_concern=None, _from_doc_delete=False, cascade_refs=None): From c88ea40b576157d6e886b1d26ef99ea09502f0c3 Mon Sep 17 00:00:00 2001 From: Tom Floyer Date: Sat, 8 Dec 2018 22:32:46 +0300 Subject: [PATCH 3/4] Updated AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 5998520a..880dfad1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -248,3 +248,4 @@ that much better: * Andy Yankovsky (https://github.com/werat) * Bastien Gérard (https://github.com/bagerard) * Trevor Hall (https://github.com/tjhall13) + * Gleb Voropaev (https://github.com/buggyspace) \ No newline at end of file From 1319e422ea236d914b72c4a90835e6f4e18d80d8 Mon Sep 17 00:00:00 2001 From: Tom Floyer Date: Sat, 8 Dec 2018 22:33:17 +0300 Subject: [PATCH 4/4] Updated docs/changelog.rst --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 30b88165..def5cc34 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Development =========== - (Fill this out as you fix issues and develop your features). +- Fix .only() working improperly after using .count() of the same instance of QuerySet ================= Changes in 0.16.3