Added QuerySet.distinct. Closes #44.

This commit is contained in:
Harry Marr 2010-07-25 15:29:02 +01:00
parent 564f950037
commit 9f98025b8c
2 changed files with 19 additions and 0 deletions

View File

@ -604,6 +604,15 @@ class QuerySet(object):
# Integer index provided # Integer index provided
elif isinstance(key, int): elif isinstance(key, int):
return self._document._from_son(self._cursor[key]) return self._document._from_son(self._cursor[key])
def distinct(self, field):
"""Return a list of distinct values for a given field.
:param field: the field to select distinct values from
.. versionadded:: 0.4
"""
return self._collection.distinct(field)
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

@ -970,6 +970,16 @@ class QuerySetTest(unittest.TestCase):
self.Person(name='ageless person').save() self.Person(name='ageless person').save()
self.assertEqual(int(self.Person.objects.sum('age')), sum(ages)) self.assertEqual(int(self.Person.objects.sum('age')), sum(ages))
def test_distinct(self):
"""Ensure that the QuerySet.distinct method works.
"""
self.Person(name='Mr Orange', age=20).save()
self.Person(name='Mr White', age=20).save()
self.Person(name='Mr Orange', age=30).save()
self.assertEqual(self.Person.objects.distinct('name'),
['Mr Orange', 'Mr White'])
self.assertEqual(self.Person.objects.distinct('age'), [20, 30])
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.
""" """