Skip to content

Commit 6f9b981

Browse files
committed
Merge pull request #79 from smacker/master
Distinct support
2 parents ed3b619 + efcebec commit 6f9b981

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

django_mongodb_engine/contrib/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def inline_map_reduce(self, *args, **kwargs):
131131
def _get_query(self):
132132
return _compiler_for_queryset(self).build_query()
133133

134+
def distinct(self, *args, **kwargs):
135+
query = self._get_query()
136+
return query.collection.distinct(*args, **kwargs)
137+
134138
class MongoDBManager(models.Manager, RawQueryMixin):
135139
"""
136140
Lets you use Map/Reduce and raw query/update with your models::
@@ -147,3 +151,6 @@ def inline_map_reduce(self, *args, **kwargs):
147151

148152
def get_query_set(self):
149153
return MongoDBQuerySet(self.model, using=self._db)
154+
155+
def distinct(self, *args, **kwargs):
156+
return self.get_query_set().distinct(*args, **kwargs)

tests/contrib/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,13 @@ def test_for_wrong_lookups(self):
194194
with self.assertRaises(DatabaseError):
195195
Post.objects.get(content_tokenized__iexact="django mongodb")
196196
Post.objects.get(content_tokenized__icontains="django mongodb")
197+
198+
199+
class DistinctTests(TestCase):
200+
def test_distinct(self):
201+
for i in xrange(10):
202+
for j in xrange(i):
203+
MapReduceModel.objects.create(n=i, m=i*2)
204+
205+
self.assertEqual(MapReduceModel.objects.distinct('m'),
206+
[2, 4, 6, 8, 10, 12, 14, 16, 18])

0 commit comments

Comments
 (0)