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

@ -115,3 +115,4 @@ that much better:
* Alexandre González
* Thomas Steinacher
* Tommi Komulainen
* Peter Landry

View File

@ -2,8 +2,9 @@
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)
- Fixed BinaryField lookup re (MongoEngine/mongoengine#48)

View File

@ -34,7 +34,9 @@ class DeReference(object):
doc_type = None
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):
doc_type = doc_type.document_type

View File

@ -1168,7 +1168,8 @@ class QuerySet(object):
.. versionchanged:: 0.5 - Fixed handling references
"""
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):
"""Load only a subset of this document's fields. ::

View File

@ -2299,6 +2299,28 @@ class QuerySetTest(unittest.TestCase):
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):
"""Ensure that custom QuerySetManager instances work as expected.
"""