From 327e164869e9fd1bda1a7065863bd1ad13981e32 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Aug 2016 08:31:19 +0300 Subject: [PATCH] Fix for #1176 -- similar to https://github.com/MongoEngine/mongoengine/pull/982 but for `update`. --- AUTHORS | 1 + README.rst | 2 +- docs/changelog.rst | 1 + mongoengine/queryset/transform.py | 6 +++++- tests/queryset/transform.py | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 8a983f86..9ed06c7a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -239,3 +239,4 @@ that much better: * Joshua Nedrud (https://github.com/Neurostack) * Shu Shen (https://github.com/shushen) * xiaost7 (https://github.com/xiaost7) + * Victor Varvaryuk diff --git a/README.rst b/README.rst index c94ec45c..547ecbd9 100644 --- a/README.rst +++ b/README.rst @@ -99,7 +99,7 @@ Some simple examples of what MongoEngine code looks like: Tests ===== To run the test suite, ensure you are running a local instance of MongoDB on -the standard port, and run: ``python setup.py nosetests``. +the standard port and have installed ``nose`` and ``rednose``, and run: ``python setup.py nosetests``. To run the test suite on every supported Python version and every supported PyMongo version, you can use ``tox``. diff --git a/docs/changelog.rst b/docs/changelog.rst index c52f2776..e71b1ff9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,6 +16,7 @@ Changes in 0.10.7 - DEV - Fixed AttributeError when initializing EmbeddedDocument with positional args. #681 - Fixed no_cursor_timeout error with pymongo 3.0+ #1304 - Replaced map-reduce based QuerySet.sum/average with aggregation-based implementations #1336 +- Fixed support for `__` to escape field names that match operators names in `update` #1351 Changes in 0.10.6 ================= diff --git a/mongoengine/queryset/transform.py b/mongoengine/queryset/transform.py index 13302afa..e5e7f83f 100644 --- a/mongoengine/queryset/transform.py +++ b/mongoengine/queryset/transform.py @@ -44,7 +44,7 @@ def query(_doc_cls=None, **kwargs): if len(parts) > 1 and parts[-1] in MATCH_OPERATORS: op = parts.pop() - # Allw to escape operator-like field name by __ + # Allow to escape operator-like field name by __ if len(parts) > 1 and parts[-1] == "": parts.pop() @@ -212,6 +212,10 @@ def update(_doc_cls=None, **update): if parts[-1] in COMPARISON_OPERATORS: match = parts.pop() + # Allow to escape operator-like field name by __ + if len(parts) > 1 and parts[-1] == "": + parts.pop() + if _doc_cls: # Switch field names to proper names [set in Field(name='foo')] try: diff --git a/tests/queryset/transform.py b/tests/queryset/transform.py index a543317a..1cb8223d 100644 --- a/tests/queryset/transform.py +++ b/tests/queryset/transform.py @@ -224,6 +224,10 @@ class TransformTest(unittest.TestCase): self.assertEqual(1, Doc.objects(item__type__="axe").count()) self.assertEqual(1, Doc.objects(item__name__="Heroic axe").count()) + Doc.objects(id=doc.id).update(set__item__type__='sword') + self.assertEqual(1, Doc.objects(item__type__="sword").count()) + self.assertEqual(0, Doc.objects(item__type__="axe").count()) + def test_understandable_error_raised(self): class Event(Document): title = StringField()