From dfa8eaf24e5fd2de6673807a8b6959f256270aa1 Mon Sep 17 00:00:00 2001 From: Eremeev Danil Date: Tue, 5 May 2015 12:00:46 +0500 Subject: [PATCH] Added changeset, updated documentation and tests, changed test condition --- docs/changelog.rst | 1 + docs/guide/querying.rst | 9 ++++++++- mongoengine/queryset/transform.py | 3 ++- tests/queryset/queryset.py | 4 +++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a27db5a4..53676562 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,6 +16,7 @@ Changes in 0.9.X - DEV - Django support was removed and will be available as a separate extension. #958 - Don't send a "cls" option to ensureIndex (related to https://jira.mongodb.org/browse/SERVER-769) - Fix for updating sorting in SortedListField. #978 +- Added __ support to escape field name in fields lookup keywords that match operators names #949 Changes in 0.9.0 ================ diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index 9d1dfd76..0a035469 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -39,6 +39,14 @@ syntax:: # been written by a user whose 'country' field is set to 'uk' uk_pages = Page.objects(author__country='uk') +.. note:: + + (version **0.9.0+**) if your field name is like mongodb operator name (for example + type, lte, lt...) and you want to place it at the end of lookup keyword + mongoengine automatically prepend $ to it. To avoid this use __ at the end of + your lookup keyword. For example if you field name is ``type`` and you want to + query by this field you must use ``.objects(user__type__="admin")`` instead of + ``.objects(user__type="admin")`` Query operators =============== @@ -663,4 +671,3 @@ following example shows how the substitutions are made:: return comments; } """) - diff --git a/mongoengine/queryset/transform.py b/mongoengine/queryset/transform.py index b08d450a..8ca3203c 100644 --- a/mongoengine/queryset/transform.py +++ b/mongoengine/queryset/transform.py @@ -44,7 +44,8 @@ def query(_doc_cls=None, _field_operation=False, **query): if len(parts) > 1 and parts[-1] in MATCH_OPERATORS: op = parts.pop() - if len(parts) > 1 and not parts[-1]: + #if user escape field name by __ + if len(parts) > 1 and parts[-1]=="": parts.pop() negate = False diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index 5d21cb0c..30c59bfa 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -4595,16 +4595,18 @@ class QuerySetTest(unittest.TestCase): def test_last_field_name_like_operator(self): class EmbeddedItem(EmbeddedDocument): type = StringField() + name = StringField() class Doc(Document): item = EmbeddedDocumentField(EmbeddedItem) Doc.drop_collection() - doc = Doc(item=EmbeddedItem(type="axe")) + doc = Doc(item=EmbeddedItem(type="axe", name="Heroic axe")) doc.save() self.assertEqual(1, Doc.objects(item__type__="axe").count()) + self.assertEqual(1, Doc.objects(item__name__="Heroic axe").count()) if __name__ == '__main__': unittest.main()