Skip to content

Commit

Permalink
Merge pull request #2670 from idoshr/fix_no_dereference_distinct
Browse files Browse the repository at this point in the history
Fix no dereference distinct
  • Loading branch information
bagerard committed Jul 17, 2022
2 parents 50a722f + c14e39e commit 564f4bd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,8 @@ def distinct(self, field):
field = self._fields_to_dbfields([field]).pop()
except LookUpError:
pass

if not self._auto_dereference:
return queryset._cursor.distinct(field)
distinct = self._dereference(
queryset._cursor.distinct(field), 1, name=field, instance=self._document
)
Expand Down
2 changes: 2 additions & 0 deletions tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,7 @@ class Bar(Document):
foo.save()

assert Foo.objects.distinct("bar") == [bar]
assert Foo.objects.no_dereference().distinct("bar") == [bar.pk]

def test_text_indexes(self):
class News(Document):
Expand Down Expand Up @@ -3711,6 +3712,7 @@ class Foo(Document):
foo.save()

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

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

0 comments on commit 564f4bd

Please sign in to comment.