Skip to content

Commit d148183

Browse files
committed
fix no_dereference not worked with distinct
1 parent 7e10bb2 commit d148183

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

mongoengine/queryset/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ def distinct(self, field):
945945
field = self._fields_to_dbfields([field]).pop()
946946
except LookUpError:
947947
pass
948-
948+
if not self._auto_dereference:
949+
return queryset._cursor.distinct(field)
949950
distinct = self._dereference(
950951
queryset._cursor.distinct(field), 1, name=field, instance=self._document
951952
)

tests/queryset/test_queryset.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -872,17 +872,15 @@ def test_rename(self):
872872
self.Person.objects.create(name="Foo", age=11)
873873

874874
bob = self.Person.objects.as_pymongo().first()
875-
assert 'age' in bob
876-
assert bob['age'] == 11
875+
assert "age" in bob
876+
assert bob["age"] == 11
877877

878-
self.Person.objects(name="Foo").update(
879-
rename__age='person_age'
880-
)
878+
self.Person.objects(name="Foo").update(rename__age="person_age")
881879

882880
bob = self.Person.objects.as_pymongo().first()
883-
assert 'age' not in bob
884-
assert 'person_age' in bob
885-
assert bob['person_age'] == 11
881+
assert "age" not in bob
882+
assert "person_age" in bob
883+
assert bob["person_age"] == 11
886884

887885
def test_save_and_only_on_fields_with_default(self):
888886
class Embed(EmbeddedDocument):
@@ -3441,6 +3439,7 @@ class Bar(Document):
34413439
foo.save()
34423440

34433441
assert Foo.objects.distinct("bar") == [bar]
3442+
assert Foo.objects.no_dereference().distinct("bar") == [bar.pk]
34443443

34453444
def test_text_indexes(self):
34463445
class News(Document):
@@ -3647,6 +3646,7 @@ class Foo(Document):
36473646
foo.save()
36483647

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

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

0 commit comments

Comments
 (0)