Added support for distinct and db_alias (MongoEngine/mongoengine#59)

This commit is contained in:
Ross Lawley 2012-08-07 10:04:05 +01:00
parent 95b1783834
commit 475488b9f2
5 changed files with 33 additions and 6 deletions

View File

@ -114,4 +114,5 @@ that much better:
* Jaime Irurzun * Jaime Irurzun
* Alexandre González * Alexandre González
* Thomas Steinacher * Thomas Steinacher
* Tommi Komulainen * Tommi Komulainen
* Peter Landry

View File

@ -2,8 +2,9 @@
Changelog Changelog
========= =========
Changes in 0.6.X Changes in 0.6.20
================ =================
- Added support for distinct and db_alias (MongoEngine/mongoengine#59)
- Improved support for chained querysets when constraining the same fields (hmarr/mongoengine#554) - Improved support for chained querysets when constraining the same fields (hmarr/mongoengine#554)
- Fixed BinaryField lookup re (MongoEngine/mongoengine#48) - Fixed BinaryField lookup re (MongoEngine/mongoengine#48)

View File

@ -34,7 +34,9 @@ class DeReference(object):
doc_type = None doc_type = None
if instance and instance._fields: if instance and instance._fields:
doc_type = instance._fields[name].field doc_type = instance._fields[name]
if hasattr(doc_type, 'field'):
doc_type = doc_type.field
if isinstance(doc_type, ReferenceField): if isinstance(doc_type, ReferenceField):
doc_type = doc_type.document_type doc_type = doc_type.document_type

View File

@ -1168,7 +1168,8 @@ class QuerySet(object):
.. versionchanged:: 0.5 - Fixed handling references .. versionchanged:: 0.5 - Fixed handling references
""" """
from dereference import DeReference from dereference import DeReference
return DeReference()(self._cursor.distinct(field), 1) return DeReference()(self._cursor.distinct(field), 1,
name=field, instance=self._document)
def only(self, *fields): def only(self, *fields):
"""Load only a subset of this document's fields. :: """Load only a subset of this document's fields. ::

View File

@ -871,7 +871,7 @@ class QuerySetTest(unittest.TestCase):
published_date__lt=datetime(2010, 1, 7, 0, 0 ,0)) published_date__lt=datetime(2010, 1, 7, 0, 0 ,0))
self.assertEqual(published_posts.count(), 2) self.assertEqual(published_posts.count(), 2)
blog_posts = BlogPost.objects blog_posts = BlogPost.objects
blog_posts = blog_posts.filter(blog__in=[blog_1, blog_2]) blog_posts = blog_posts.filter(blog__in=[blog_1, blog_2])
blog_posts = blog_posts.filter(blog=blog_3) blog_posts = blog_posts.filter(blog=blog_3)
@ -2299,6 +2299,28 @@ class QuerySetTest(unittest.TestCase):
self.assertEquals(Foo.objects.distinct("bar"), [bar]) self.assertEquals(Foo.objects.distinct("bar"), [bar])
def test_distinct_handles_references_to_alias(self):
register_connection('testdb', 'mongoenginetest2')
class Foo(Document):
bar = ReferenceField("Bar")
meta = {'db_alias': 'testdb'}
class Bar(Document):
text = StringField()
meta = {'db_alias': 'testdb'}
Bar.drop_collection()
Foo.drop_collection()
bar = Bar(text="hi")
bar.save()
foo = Foo(bar=bar)
foo.save()
self.assertEquals(Foo.objects.distinct("bar"), [bar])
def test_custom_manager(self): def test_custom_manager(self):
"""Ensure that custom QuerySetManager instances work as expected. """Ensure that custom QuerySetManager instances work as expected.
""" """