@@ -25,13 +25,16 @@ def check_django_compatability():
2525 )
2626
2727
28- class CollectionDebugWrapper :
29- def __init__ (self , collection , db ):
28+ class OperationDebugWrapper :
29+ def __init__ (self , db , collection = None ):
3030 self .collection = collection
3131 self .db = db
32+ use_collection = collection is not None
33+ self .collection_name = f"{ collection .name } ." if use_collection else ""
34+ self .wrapped = self .collection if use_collection else self .db .database
3235
3336 def __getattr__ (self , attr ):
34- return getattr (self .collection , attr )
37+ return getattr (self .wrapped , attr )
3538
3639 def profile_call (self , func , args = (), kwargs = None ):
3740 start = time .monotonic ()
@@ -44,7 +47,7 @@ def log(self, op, duration, args, kwargs=None):
4447 # added to this logging.
4548 msg = "(%.3f) %s"
4649 args = ", " .join (str (arg ) for arg in args )
47- operation = f"{ self .collection . name } . { op } ({ args } )"
50+ operation = f"db. { self .collection_name } { op } ({ args } )"
4851 if len (settings .DATABASES ) > 1 :
4952 msg += f"; alias={ self .db .alias } "
5053 self .db .queries_log .append (
@@ -66,7 +69,7 @@ def log(self, op, duration, args, kwargs=None):
6669
6770 def logging_wrapper (method ):
6871 def wrapper (self , * args , ** kwargs ):
69- func = getattr (self .collection , method )
72+ func = getattr (self .wrapped , method )
7073 # Collection.insert_many() mutates args (the documents) by adding
7174 # _id. deepcopy() to avoid logging that version.
7275 original_args = copy .deepcopy (args )
@@ -78,6 +81,7 @@ def wrapper(self, *args, **kwargs):
7881
7982 # These are the operations that this backend uses.
8083 aggregate = logging_wrapper ("aggregate" )
84+ create_collection = logging_wrapper ("create_collection" )
8185 drop = logging_wrapper ("drop" )
8286 insert_many = logging_wrapper ("insert_many" )
8387 delete_many = logging_wrapper ("delete_many" )
0 commit comments