Skip to content

Commit 0fdd743

Browse files
committed
Merge branch 'master' of github.com:MongoEngine/mongoengine into mcsheehan-2507-support-mongodb-aws
2 parents f88dff2 + 51afeca commit 0fdd743

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Development
88
===========
99
- (Fill this out as you fix issues and develop your features).
1010
- Support MONGODB-AWS authentication mechanism (with `authmechanismproperties`) #2507
11+
- Turning off dereferencing for the results of distinct query. #2663
1112

1213
Changes in 0.24.2
1314
=================

docs/guide/defining-documents.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ arguments can be set on all fields:
113113
:attr:`db_field` (Default: None)
114114
The MongoDB field name.
115115

116+
If set, operations in MongoDB will be performed with this value instead of the class attribute.
117+
118+
This allows you to use a different attribute than the name of the field used in MongoDB. ::
119+
120+
from mongoengine import *
121+
122+
class Page(Document):
123+
page_number = IntField(db_field="pageNumber")
124+
125+
# Create a Page and save it
126+
Page(page_number=1).save()
127+
128+
# How 'pageNumber' is stored in MongoDB
129+
Page.objects.as_pymongo() # [{'_id': ObjectId('629dfc45ee4cc407b1586b1f'), 'pageNumber': 1}]
130+
131+
# Retrieve the object
132+
page: Page = Page.objects.first()
133+
134+
print(page.page_number) # prints 1
135+
136+
print(page.pageNumber) # raises AttributeError
137+
138+
.. note:: If set, use the name of the attribute when defining indexes in the :attr:`meta`
139+
dictionary rather than the :attr:`db_field` otherwise, :class:`~mongoengine.LookUpError`
140+
will be raised.
141+
142+
116143
:attr:`required` (Default: False)
117144
If set to True and the field is not set on the document instance, a
118145
:class:`~mongoengine.ValidationError` will be raised when the document is

mongoengine/queryset/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,12 @@ def distinct(self, field):
953953
field = self._fields_to_dbfields([field]).pop()
954954
except LookUpError:
955955
pass
956+
957+
raw_values = queryset._cursor.distinct(field)
956958
if not self._auto_dereference:
957-
return queryset._cursor.distinct(field)
958-
distinct = self._dereference(
959-
queryset._cursor.distinct(field), 1, name=field, instance=self._document
960-
)
959+
return raw_values
960+
961+
distinct = self._dereference(raw_values, 1, name=field, instance=self._document)
961962

962963
doc_field = self._document._fields.get(field.split(".", 1)[0])
963964
instance = None

0 commit comments

Comments
 (0)