Added __len__ to QuerySet
This commit is contained in:
parent
9bfe5c7a49
commit
17aef253cb
13
README.rst
13
README.rst
@ -19,7 +19,8 @@ Dependencies
|
||||
|
||||
Examples
|
||||
========
|
||||
::
|
||||
Some simple examples of what MongoEngine code looks like::
|
||||
|
||||
class BlogPost(Document):
|
||||
title = StringField(required=True, max_length=200)
|
||||
posted = DateTimeField(default=datetime.datetime.now)
|
||||
@ -56,15 +57,15 @@ Examples
|
||||
=== MongoEngine Docs ===
|
||||
Link: hmarr.com/mongoengine
|
||||
|
||||
>>> BlogPost.objects.count()
|
||||
>>> len(BlogPost.objects)
|
||||
2
|
||||
>>> HtmlPost.objects.count()
|
||||
>>> len(HtmlPost.objects)
|
||||
1
|
||||
>>> LinkPost.objects.count()
|
||||
>>> len(LinkPost.objects)
|
||||
1
|
||||
|
||||
# Find tagged posts
|
||||
>>> BlogPost.objects(tags='mongoengine').count()
|
||||
>>> len(BlogPost.objects(tags='mongoengine'))
|
||||
2
|
||||
>>> BlogPost.objects(tags='mongodb').count()
|
||||
>>> len(BlogPost.objects(tags='mongodb'))
|
||||
1
|
||||
|
@ -100,6 +100,9 @@ class QuerySet(object):
|
||||
"""
|
||||
return self._cursor.count()
|
||||
|
||||
def __len__(self):
|
||||
return self.count()
|
||||
|
||||
def limit(self, n):
|
||||
"""Limit the number of returned documents to `n`. This may also be
|
||||
achieved using array-slicing syntax (e.g. ``User.objects[:5]``).
|
||||
|
@ -235,9 +235,9 @@ class DocumentTest(unittest.TestCase):
|
||||
"""
|
||||
person = self.Person(name="Test User", age=30)
|
||||
person.save()
|
||||
self.assertEqual(self.Person.objects.count(), 1)
|
||||
self.assertEqual(len(self.Person.objects), 1)
|
||||
person.delete()
|
||||
self.assertEqual(self.Person.objects.count(), 0)
|
||||
self.assertEqual(len(self.Person.objects), 0)
|
||||
|
||||
def test_save_custom_id(self):
|
||||
"""Ensure that a document may be saved with a custom _id.
|
||||
|
@ -50,7 +50,7 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
# Find all people in the collection
|
||||
people = self.Person.objects
|
||||
self.assertEqual(people.count(), 2)
|
||||
self.assertEqual(len(people), 2)
|
||||
results = list(people)
|
||||
self.assertTrue(isinstance(results[0], self.Person))
|
||||
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
|
||||
people = self.Person.objects(age=20)
|
||||
self.assertEqual(people.count(), 1)
|
||||
self.assertEqual(len(people), 1)
|
||||
person = people.next()
|
||||
self.assertEqual(person.name, "User A")
|
||||
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 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.assertEqual(self.Person.objects.count(), 2)
|
||||
self.assertEqual(len(self.Person.objects), 2)
|
||||
|
||||
self.Person.objects.delete()
|
||||
self.assertEqual(self.Person.objects.count(), 0)
|
||||
self.assertEqual(len(self.Person.objects), 0)
|
||||
|
||||
def test_order_by(self):
|
||||
"""Ensure that QuerySets may be ordered.
|
||||
|
Loading…
x
Reference in New Issue
Block a user