Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implementation of distinct() #120

Closed
wants to merge 1 commit into from

3 participants

@dconlon

Hi All,

I saw that distinct() was added (efcebec) but did not have cause to use it until today.

It seemed possible to chain distinct() with filter():

from django_mongodb_engine.contrib import MongoDBManager

class FooModel(models.Model):
    ...
    objects = MongoDBManager()

FooModel.objects.filter(filters).distinct(fields)

in that code of the above form executes without error.

However the filters are ignored as distinct() is called on the pymongo Collection, not on the pymongo Cursor from the query result.

I'm not sure what the intention was with distinct() but I lost several hours to this today and wondered whether it should either be amended to work with filter() or raise an exception when called on a QuerySet. Either could help future hapless coders like me.

I've had a go at making it work with filter() and welcome your feedback.

Dan

@charettes
Owner

Hi @dconlon, thanks for the bug report and the provided pull request.

I'm surprised this doesn't work as expected, I agree this is a bug. Is it possible for you to provide a patch against develop branch instead of master? We use the latter for stable release thus all bug corrections and new feature should be merged into develop first.

@jonashaag

Edit: Bullshit. See my next post.

Oh gosh. That bug would've been found by any static analysis tool. The issue is that we're not passing query to distinct as first parameter.

@charettes charettes closed this
@charettes
Owner

@dconlon I'll close this pull request since it's against master anyway. Do you mind opening a new one against develop branch with your test case and @jonashaag's suggested fix?

@jonashaag

Wait that's nonsense. We actually use query in the next line. I totally missed that. So the original fix looks right. It just needs to be opened against develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  django_mongodb_engine/contrib/__init__.py 100644 → 100755
@@ -133,7 +133,7 @@ def _get_query(self):
def distinct(self, *args, **kwargs):
query = self._get_query()
- return query.collection.distinct(*args, **kwargs)
+ return query._get_results().distinct(*args, **kwargs)
class MongoDBManager(models.Manager, RawQueryMixin):
"""
View
2  tests/contrib/tests.py 100644 → 100755
@@ -204,3 +204,5 @@ def test_distinct(self):
self.assertEqual(MapReduceModel.objects.distinct('m'),
[2, 4, 6, 8, 10, 12, 14, 16, 18])
+
+ self.assertEqual(MapReduceModel.objects.filter(n=6).distinct('m'), [12])
Something went wrong with that request. Please try again.