@@ -25,13 +25,15 @@ def check_django_compatability():
25
25
)
26
26
27
27
28
- class CollectionDebugWrapper :
29
- def __init__ (self , collection , db ):
28
+ class OperationDebugWrapper :
29
+ def __init__ (self , db , collection = None ):
30
30
self .collection = collection
31
31
self .db = db
32
32
33
33
def __getattr__ (self , attr ):
34
- return getattr (self .collection , attr )
34
+ if self .collection is not None :
35
+ return getattr (self .collection , attr )
36
+ return getattr (self .db , attr )
35
37
36
38
def profile_call (self , func , args = (), kwargs = None ):
37
39
start = time .monotonic ()
@@ -44,7 +46,8 @@ def log(self, op, duration, args, kwargs=None):
44
46
# added to this logging.
45
47
msg = "(%.3f) %s"
46
48
args = ", " .join (str (arg ) for arg in args )
47
- operation = f"{ self .collection .name } .{ op } ({ args } )"
49
+ collection_name = f"{ self .collection .name } ." if self .collection is not None else ""
50
+ operation = f"db.{ collection_name } { op } ({ args } )"
48
51
if len (settings .DATABASES ) > 1 :
49
52
msg += f"; alias={ self .db .alias } "
50
53
self .db .queries_log .append (
@@ -66,7 +69,10 @@ def log(self, op, duration, args, kwargs=None):
66
69
67
70
def logging_wrapper (method ):
68
71
def wrapper (self , * args , ** kwargs ):
69
- func = getattr (self .collection , method )
72
+ if self .collection is not None :
73
+ func = getattr (self .collection , method )
74
+ else :
75
+ func = getattr (self .db .database , method )
70
76
# Collection.insert_many() mutates args (the documents) by adding
71
77
# _id. deepcopy() to avoid logging that version.
72
78
original_args = copy .deepcopy (args )
@@ -78,6 +84,7 @@ def wrapper(self, *args, **kwargs):
78
84
79
85
# These are the operations that this backend uses.
80
86
aggregate = logging_wrapper ("aggregate" )
87
+ create_collection = logging_wrapper ("create_collection" )
81
88
drop = logging_wrapper ("drop" )
82
89
insert_many = logging_wrapper ("insert_many" )
83
90
delete_many = logging_wrapper ("delete_many" )
0 commit comments