added queryset chaining via 'filter' method. test included.
This commit is contained in:
parent
42a58dda57
commit
cfc394963f
@ -19,6 +19,7 @@ class BaseField(object):
|
|||||||
self.default = default
|
self.default = default
|
||||||
self.unique = bool(unique or unique_with)
|
self.unique = bool(unique or unique_with)
|
||||||
self.unique_with = unique_with
|
self.unique_with = unique_with
|
||||||
|
self._loaded = []
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
"""Descriptor for retrieving a value from a field in a document. Do
|
"""Descriptor for retrieving a value from a field in a document. Do
|
||||||
@ -68,7 +69,7 @@ class ObjectIdField(BaseField):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
return str(value)
|
return unicode(value)
|
||||||
|
|
||||||
def to_mongo(self, value):
|
def to_mongo(self, value):
|
||||||
if not isinstance(value, pymongo.objectid.ObjectId):
|
if not isinstance(value, pymongo.objectid.ObjectId):
|
||||||
|
@ -62,6 +62,9 @@ class QuerySet(object):
|
|||||||
query = QuerySet._transform_query(_doc_cls=self._document, **query)
|
query = QuerySet._transform_query(_doc_cls=self._document, **query)
|
||||||
self._query.update(query)
|
self._query.update(query)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def filter(self, **query):
|
||||||
|
return self.__call__(**query)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _collection(self):
|
def _collection(self):
|
||||||
|
@ -131,6 +131,42 @@ 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")
|
||||||
|
|
||||||
|
def test_filter_chaining(self):
|
||||||
|
"""Ensure filters can be chained together.
|
||||||
|
"""
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class BlogPost(Document):
|
||||||
|
title = StringField()
|
||||||
|
is_published = BooleanField()
|
||||||
|
published_date = DateTimeField()
|
||||||
|
|
||||||
|
@queryset_manager
|
||||||
|
def published(queryset):
|
||||||
|
return queryset(is_published=True)
|
||||||
|
|
||||||
|
blog_post_1 = BlogPost(title="Blog Post #1",
|
||||||
|
is_published = True,
|
||||||
|
published_date=datetime(2010, 1, 5, 0, 0 ,0))
|
||||||
|
blog_post_2 = BlogPost(title="Blog Post #2",
|
||||||
|
is_published = True,
|
||||||
|
published_date=datetime(2010, 1, 6, 0, 0 ,0))
|
||||||
|
blog_post_3 = BlogPost(title="Blog Post #3",
|
||||||
|
is_published = True,
|
||||||
|
published_date=datetime(2010, 1, 7, 0, 0 ,0))
|
||||||
|
|
||||||
|
blog_post_1.save()
|
||||||
|
blog_post_2.save()
|
||||||
|
blog_post_3.save()
|
||||||
|
|
||||||
|
# find all published blog posts before 2010-01-07
|
||||||
|
published_posts = BlogPost.published()
|
||||||
|
published_posts = published_posts.filter(
|
||||||
|
published_date__lt=datetime(2010, 1, 7, 0, 0 ,0))
|
||||||
|
self.assertEqual(published_posts.count(), 2)
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
def test_ordering(self):
|
def test_ordering(self):
|
||||||
"""Ensure default ordering is applied and can be overridden.
|
"""Ensure default ordering is applied and can be overridden.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user