Updated with_id to raise Error if used with a filter.

Closes #365
This commit is contained in:
Ross Lawley 2011-12-02 07:11:06 -08:00
parent a6948771d8
commit e9d7353294
4 changed files with 11 additions and 1 deletions

View File

@ -5,6 +5,7 @@ Changelog
Changes in dev Changes in dev
============== ==============
- Added InvalidQueryError when calling with_id with a filter
- Added support for DBRefs in distinct() - Added support for DBRefs in distinct()
- Fixed issue saving False booleans - Fixed issue saving False booleans
- Fixed issue with dynamic documents deltas - Fixed issue with dynamic documents deltas

View File

@ -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._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 0.4 to 0.5
=========== ===========

View File

@ -858,10 +858,16 @@ class QuerySet(object):
return return_one and results[0] or results return return_one and results[0] or results
def with_id(self, object_id): 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 :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() return self._document.objects(pk=object_id).first()
def in_bulk(self, object_ids): def in_bulk(self, object_ids):

View File

@ -154,6 +154,8 @@ class QuerySetTest(unittest.TestCase):
person = self.Person.objects.with_id(person1.id) person = self.Person.objects.with_id(person1.id)
self.assertEqual(person.name, "User A") 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): def test_find_only_one(self):
"""Ensure that a query using ``get`` returns at most one result. """Ensure that a query using ``get`` returns at most one result.
""" """