diff --git a/docs/changelog.rst b/docs/changelog.rst index e42e390d..2e5f1a3e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Added InvalidQueryError when calling with_id with a filter - Added support for DBRefs in distinct() - Fixed issue saving False booleans - Fixed issue with dynamic documents deltas diff --git a/docs/upgrade.rst b/docs/upgrade.rst index cb72b356..d406688c 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -13,6 +13,7 @@ an InvalidDocument error as they aren't currently supported. Document._get_subclasses - Is no longer used and the class method has been removed. +Document.objects.with_id - now raises an InvalidQueryError if used with a filter. 0.4 to 0.5 =========== diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index a7a37984..ce9b175c 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -858,10 +858,16 @@ class QuerySet(object): return return_one and results[0] or results def with_id(self, object_id): - """Retrieve the object matching the id provided. + """Retrieve the object matching the id provided. Uses `object_id` only + and raises InvalidQueryError if a filter has been applied. :param object_id: the value for the id of the document to look up + + .. versionchanged:: 0.6 Raises InvalidQueryError if filter has been set """ + if not self._query_obj.empty: + raise InvalidQueryError("Cannot use a filter whilst using `with_id`") + return self._document.objects(pk=object_id).first() def in_bulk(self, object_ids): diff --git a/tests/queryset.py b/tests/queryset.py index c1282405..37fa5247 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -154,6 +154,8 @@ class QuerySetTest(unittest.TestCase): person = self.Person.objects.with_id(person1.id) self.assertEqual(person.name, "User A") + self.assertRaises(InvalidQueryError, self.Person.objects(name="User A").with_id, person1.id) + def test_find_only_one(self): """Ensure that a query using ``get`` returns at most one result. """