diff --git a/AUTHORS b/AUTHORS index fe62026d..9d31c955 100644 --- a/AUTHORS +++ b/AUTHORS @@ -128,4 +128,6 @@ that much better: * Peter Teichman * Jakub Kot * Jorge Bastida - * Aleksandr Sorokoumov \ No newline at end of file + * Aleksandr Sorokoumov + * Yohan Graterol + * bool-dev \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index 8619a635..667413e2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,7 @@ Changelog Changes in 0.7.10 ================= +- Resolve field name to db field name when using distinct(#260, #264, #269) - Added kwargs to doc.save to help interop with django (#223, #270) - Fixed cloning querysets in PY3 - Int fields no longer unset in save when changed to 0 (#272) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 20b18b7e..4aeff833 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -1212,8 +1212,11 @@ class QuerySet(object): .. versionchanged:: 0.5 - Fixed handling references .. versionchanged:: 0.6 - Improved db_field refrence handling """ - return self._dereference(self._cursor.distinct(field), 1, - name=field, instance=self._document) + try: + field = self._fields_to_dbfields([field]).pop() + finally: + return self._dereference(self._cursor.distinct(field), 1, + name=field, instance=self._document) def only(self, *fields): """Load only a subset of this document's fields. :: diff --git a/tests/test_queryset.py b/tests/test_queryset.py index 43bb70bc..88daa5fa 100644 --- a/tests/test_queryset.py +++ b/tests/test_queryset.py @@ -2486,6 +2486,25 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(Foo.objects.distinct("bar"), [bar]) + def test_distinct_handles_db_field(self): + """Ensure that distinct resolves field name to db_field as expected. + """ + class Product(Document): + product_id = IntField(db_field='pid') + + Product.drop_collection() + + Product(product_id=1).save() + Product(product_id=2).save() + Product(product_id=1).save() + + self.assertEqual(set(Product.objects.distinct('product_id')), + set([1, 2])) + self.assertEqual(set(Product.objects.distinct('pid')), + set([1, 2])) + + Product.drop_collection() + def test_custom_manager(self): """Ensure that custom QuerySetManager instances work as expected. """