Added __len__ to QuerySet

This commit is contained in:
Harry Marr 2009-12-24 18:45:35 +00:00
parent 9bfe5c7a49
commit 17aef253cb
4 changed files with 17 additions and 13 deletions

View File

@ -19,7 +19,8 @@ Dependencies
Examples Examples
======== ========
:: Some simple examples of what MongoEngine code looks like::
class BlogPost(Document): class BlogPost(Document):
title = StringField(required=True, max_length=200) title = StringField(required=True, max_length=200)
posted = DateTimeField(default=datetime.datetime.now) posted = DateTimeField(default=datetime.datetime.now)
@ -56,15 +57,15 @@ Examples
=== MongoEngine Docs === === MongoEngine Docs ===
Link: hmarr.com/mongoengine Link: hmarr.com/mongoengine
>>> BlogPost.objects.count() >>> len(BlogPost.objects)
2 2
>>> HtmlPost.objects.count() >>> len(HtmlPost.objects)
1 1
>>> LinkPost.objects.count() >>> len(LinkPost.objects)
1 1
# Find tagged posts # Find tagged posts
>>> BlogPost.objects(tags='mongoengine').count() >>> len(BlogPost.objects(tags='mongoengine'))
2 2
>>> BlogPost.objects(tags='mongodb').count() >>> len(BlogPost.objects(tags='mongodb'))
1 1

View File

@ -100,6 +100,9 @@ class QuerySet(object):
""" """
return self._cursor.count() return self._cursor.count()
def __len__(self):
return self.count()
def limit(self, n): def limit(self, n):
"""Limit the number of returned documents to `n`. This may also be """Limit the number of returned documents to `n`. This may also be
achieved using array-slicing syntax (e.g. ``User.objects[:5]``). achieved using array-slicing syntax (e.g. ``User.objects[:5]``).

View File

@ -235,9 +235,9 @@ class DocumentTest(unittest.TestCase):
""" """
person = self.Person(name="Test User", age=30) person = self.Person(name="Test User", age=30)
person.save() person.save()
self.assertEqual(self.Person.objects.count(), 1) self.assertEqual(len(self.Person.objects), 1)
person.delete() person.delete()
self.assertEqual(self.Person.objects.count(), 0) self.assertEqual(len(self.Person.objects), 0)
def test_save_custom_id(self): def test_save_custom_id(self):
"""Ensure that a document may be saved with a custom _id. """Ensure that a document may be saved with a custom _id.

View File

@ -50,7 +50,7 @@ class QuerySetTest(unittest.TestCase):
# Find all people in the collection # Find all people in the collection
people = self.Person.objects people = self.Person.objects
self.assertEqual(people.count(), 2) self.assertEqual(len(people), 2)
results = list(people) results = list(people)
self.assertTrue(isinstance(results[0], self.Person)) self.assertTrue(isinstance(results[0], self.Person))
self.assertTrue(isinstance(results[0].id, (pymongo.objectid.ObjectId, self.assertTrue(isinstance(results[0].id, (pymongo.objectid.ObjectId,
@ -62,7 +62,7 @@ class QuerySetTest(unittest.TestCase):
# Use a query to filter the people found to just person1 # Use a query to filter the people found to just person1
people = self.Person.objects(age=20) people = self.Person.objects(age=20)
self.assertEqual(people.count(), 1) self.assertEqual(len(people), 1)
person = people.next() person = people.next()
self.assertEqual(person.name, "User A") self.assertEqual(person.name, "User A")
self.assertEqual(person.age, 20) self.assertEqual(person.age, 20)
@ -158,13 +158,13 @@ class QuerySetTest(unittest.TestCase):
self.Person(name="User B", age=30).save() self.Person(name="User B", age=30).save()
self.Person(name="User C", age=40).save() self.Person(name="User C", age=40).save()
self.assertEqual(self.Person.objects.count(), 3) self.assertEqual(len(self.Person.objects), 3)
self.Person.objects(age__lt=30).delete() self.Person.objects(age__lt=30).delete()
self.assertEqual(self.Person.objects.count(), 2) self.assertEqual(len(self.Person.objects), 2)
self.Person.objects.delete() self.Person.objects.delete()
self.assertEqual(self.Person.objects.count(), 0) self.assertEqual(len(self.Person.objects), 0)
def test_order_by(self): def test_order_by(self):
"""Ensure that QuerySets may be ordered. """Ensure that QuerySets may be ordered.