@@ -962,6 +962,14 @@ def read_preference(self, read_preference):
962
962
queryset ._cursor_obj = None # we need to re-create the cursor object whenever we apply read_preference
963
963
return queryset
964
964
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
+
965
973
def scalar (self , * fields ):
966
974
"""Instead of returning Document instances, return either a specific
967
975
value or a tuple of values in order.
@@ -1604,3 +1612,21 @@ def _ensure_indexes(self):
1604
1612
"Use Doc.ensure_indexes() instead." )
1605
1613
warnings .warn (msg , DeprecationWarning )
1606
1614
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