Skip to content

Commit 564f4bd

Browse files
authored
Merge pull request #2670 from idoshr/fix_no_dereference_distinct
Fix no dereference distinct
2 parents 50a722f + c14e39e commit 564f4bd

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

mongoengine/queryset/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,8 @@ def distinct(self, field):
953953
field = self._fields_to_dbfields([field]).pop()
954954
except LookUpError:
955955
pass
956-
956+
if not self._auto_dereference:
957+
return queryset._cursor.distinct(field)
957958
distinct = self._dereference(
958959
queryset._cursor.distinct(field), 1, name=field, instance=self._document
959960
)

tests/queryset/test_queryset.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,7 @@ class Bar(Document):
35053505
foo.save()
35063506

35073507
assert Foo.objects.distinct("bar") == [bar]
3508+
assert Foo.objects.no_dereference().distinct("bar") == [bar.pk]
35083509

35093510
def test_text_indexes(self):
35103511
class News(Document):
@@ -3711,6 +3712,7 @@ class Foo(Document):
37113712
foo.save()
37123713

37133714
assert Foo.objects.distinct("bar_lst") == [bar_1, bar_2]
3715+
assert Foo.objects.no_dereference().distinct("bar_lst") == [bar_1.pk, bar_2.pk]
37143716

37153717
def test_custom_manager(self):
37163718
"""Ensure that custom QuerySetManager instances work as expected."""

0 commit comments

Comments
 (0)