From a6948771d8b95920ddaf0639f7662c65eaf43376 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 2 Dec 2011 06:47:58 -0800 Subject: [PATCH] Added ReferencField handling with .distinct() Closes #356 --- AUTHORS | 1 + docs/changelog.rst | 1 + mongoengine/queryset.py | 4 +++- tests/queryset.py | 24 +++++++++++++++++++++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 5dbd4d7c..055a2b98 100644 --- a/AUTHORS +++ b/AUTHORS @@ -84,3 +84,4 @@ that much better: * dave mankoff * Alexander G. Morano * jwilder + * Joe Shaw diff --git a/docs/changelog.rst b/docs/changelog.rst index 157409e3..e42e390d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Added support for DBRefs in distinct() - Fixed issue saving False booleans - Fixed issue with dynamic documents deltas - Added Reverse Delete Rule support to ListFields - MapFields aren't supported diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 0da17180..a7a37984 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -1073,8 +1073,10 @@ class QuerySet(object): :param field: the field to select distinct values from .. versionadded:: 0.4 + .. versionchanged:: 0.5 - Fixed handling references """ - return self._cursor.distinct(field) + from dereference import dereference + return dereference(self._cursor.distinct(field), 1) def only(self, *fields): """Load only a subset of this document's fields. :: diff --git a/tests/queryset.py b/tests/queryset.py index d978cf28..c1282405 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -1958,6 +1958,24 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(set(self.Person.objects(age=30).distinct('name')), set(['Mr Orange', 'Mr Pink'])) + def test_distinct_handles_references(self): + class Foo(Document): + bar = ReferenceField("Bar") + + class Bar(Document): + text = StringField() + + 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. """ @@ -2870,7 +2888,7 @@ class QueryFieldListTest(unittest.TestCase): class Foo(EmbeddedDocument): shape = StringField() color = StringField() - trick = BooleanField() + trick = BooleanField() meta = {'allow_inheritance': False} class Bar(Document): @@ -2878,7 +2896,7 @@ class QueryFieldListTest(unittest.TestCase): meta = {'allow_inheritance': False} Bar.drop_collection() - + b1 = Bar(foo=[Foo(shape= "square", color ="purple", thick = False), Foo(shape= "circle", color ="red", thick = True)]) b1.save() @@ -2886,7 +2904,7 @@ class QueryFieldListTest(unittest.TestCase): b2 = Bar(foo=[Foo(shape= "square", color ="red", thick = True), Foo(shape= "circle", color ="purple", thick = False)]) b2.save() - + ak = list(Bar.objects(foo__match={'shape': "square", "color": "purple"})) self.assertEqual([b1], ak)