Added ReferencField handling with .distinct()

Closes #356
This commit is contained in:
Ross Lawley 2011-12-02 06:47:58 -08:00
parent 403977cd49
commit a6948771d8
4 changed files with 26 additions and 4 deletions

View File

@ -84,3 +84,4 @@ that much better:
* dave mankoff * dave mankoff
* Alexander G. Morano * Alexander G. Morano
* jwilder * jwilder
* Joe Shaw

View File

@ -5,6 +5,7 @@ Changelog
Changes in dev Changes in dev
============== ==============
- Added support for DBRefs in distinct()
- Fixed issue saving False booleans - Fixed issue saving False booleans
- Fixed issue with dynamic documents deltas - Fixed issue with dynamic documents deltas
- Added Reverse Delete Rule support to ListFields - MapFields aren't supported - Added Reverse Delete Rule support to ListFields - MapFields aren't supported

View File

@ -1073,8 +1073,10 @@ class QuerySet(object):
:param field: the field to select distinct values from :param field: the field to select distinct values from
.. versionadded:: 0.4 .. 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): def only(self, *fields):
"""Load only a subset of this document's fields. :: """Load only a subset of this document's fields. ::

View File

@ -1958,6 +1958,24 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(set(self.Person.objects(age=30).distinct('name')), self.assertEqual(set(self.Person.objects(age=30).distinct('name')),
set(['Mr Orange', 'Mr Pink'])) 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): def test_custom_manager(self):
"""Ensure that custom QuerySetManager instances work as expected. """Ensure that custom QuerySetManager instances work as expected.
""" """
@ -2870,7 +2888,7 @@ class QueryFieldListTest(unittest.TestCase):
class Foo(EmbeddedDocument): class Foo(EmbeddedDocument):
shape = StringField() shape = StringField()
color = StringField() color = StringField()
trick = BooleanField() trick = BooleanField()
meta = {'allow_inheritance': False} meta = {'allow_inheritance': False}
class Bar(Document): class Bar(Document):
@ -2878,7 +2896,7 @@ class QueryFieldListTest(unittest.TestCase):
meta = {'allow_inheritance': False} meta = {'allow_inheritance': False}
Bar.drop_collection() Bar.drop_collection()
b1 = Bar(foo=[Foo(shape= "square", color ="purple", thick = False), b1 = Bar(foo=[Foo(shape= "square", color ="purple", thick = False),
Foo(shape= "circle", color ="red", thick = True)]) Foo(shape= "circle", color ="red", thick = True)])
b1.save() b1.save()
@ -2886,7 +2904,7 @@ class QueryFieldListTest(unittest.TestCase):
b2 = Bar(foo=[Foo(shape= "square", color ="red", thick = True), b2 = Bar(foo=[Foo(shape= "square", color ="red", thick = True),
Foo(shape= "circle", color ="purple", thick = False)]) Foo(shape= "circle", color ="purple", thick = False)])
b2.save() b2.save()
ak = list(Bar.objects(foo__match={'shape': "square", "color": "purple"})) ak = list(Bar.objects(foo__match={'shape': "square", "color": "purple"}))
self.assertEqual([b1], ak) self.assertEqual([b1], ak)