Skip to content

Commit 60989ed

Browse files
committed
Add comment method to QuerySet class
Inspired by #19
1 parent 15e4cb8 commit 60989ed

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

mongoengine/queryset/queryset.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,14 @@ def read_preference(self, read_preference):
962962
queryset._cursor_obj = None # we need to re-create the cursor object whenever we apply read_preference
963963
return queryset
964964

965+
def comment(self, text):
966+
"""Add a comment to the query.
967+
968+
See https://docs.mongodb.com/manual/reference/method/cursor.comment/#cursor.comment
969+
for details.
970+
"""
971+
return self._chainable_method("comment", text)
972+
965973
def scalar(self, *fields):
966974
"""Instead of returning Document instances, return either a specific
967975
value or a tuple of values in order.
@@ -1604,3 +1612,21 @@ def _ensure_indexes(self):
16041612
"Use Doc.ensure_indexes() instead.")
16051613
warnings.warn(msg, DeprecationWarning)
16061614
self._document.__class__.ensure_indexes()
1615+
1616+
def _chainable_method(self, method_name, val):
1617+
"""Call a particular method on the PyMongo cursor call a particular chainable method
1618+
with the provided value.
1619+
"""
1620+
queryset = self.clone()
1621+
1622+
# Get an existing cursor object or create a new one
1623+
cursor = queryset._cursor
1624+
1625+
# Find the requested method on the cursor and call it with the
1626+
# provided value
1627+
getattr(cursor, method_name)(val)
1628+
1629+
# Cache the value on the queryset._{method_name}
1630+
setattr(queryset, '_' + method_name, val)
1631+
1632+
return queryset

0 commit comments

Comments
 (0)