4
4
5
5
6
6
class DatabaseSchemaEditor (BaseDatabaseSchemaEditor ):
7
+ def get_collection (self , name ):
8
+ return self .connection .get_collection (name )
9
+
7
10
@wrap_database_errors
8
11
def create_model (self , model ):
9
12
self .connection .database .create_collection (model ._meta .db_table )
@@ -17,7 +20,7 @@ def delete_model(self, model):
17
20
for field in model ._meta .local_many_to_many :
18
21
if field .remote_field .through ._meta .auto_created :
19
22
self .delete_model (field .remote_field .through )
20
- self .connection . database [ model ._meta .db_table ] .drop ()
23
+ self .get_collection ( model ._meta .db_table ) .drop ()
21
24
22
25
def add_field (self , model , field ):
23
26
# Create implicit M2M tables.
@@ -26,7 +29,7 @@ def add_field(self, model, field):
26
29
return
27
30
# Set default value on existing documents.
28
31
if column := field .column :
29
- self .connection . database [ model ._meta .db_table ] .update_many (
32
+ self .get_collection ( model ._meta .db_table ) .update_many (
30
33
{}, [{"$set" : {column : self .effective_default (field )}}]
31
34
)
32
35
@@ -41,7 +44,7 @@ def _alter_field(
41
44
new_db_params ,
42
45
strict = False ,
43
46
):
44
- collection = self .connection . database [ model ._meta .db_table ]
47
+ collection = self .get_collection ( model ._meta .db_table )
45
48
# Have they renamed the column?
46
49
if old_field .column != new_field .column :
47
50
collection .update_many ({}, {"$rename" : {old_field .column : new_field .column }})
@@ -59,7 +62,7 @@ def remove_field(self, model, field):
59
62
return
60
63
# Unset field on existing documents.
61
64
if column := field .column :
62
- self .connection . database [ model ._meta .db_table ] .update_many ({}, {"$unset" : {column : "" }})
65
+ self .get_collection ( model ._meta .db_table ) .update_many ({}, {"$unset" : {column : "" }})
63
66
64
67
def alter_index_together (self , model , old_index_together , new_index_together ):
65
68
pass
@@ -85,4 +88,4 @@ def remove_constraint(self, model, constraint):
85
88
def alter_db_table (self , model , old_db_table , new_db_table ):
86
89
if old_db_table == new_db_table :
87
90
return
88
- self .connection . database [ old_db_table ] .rename (new_db_table )
91
+ self .get_collection ( old_db_table ) .rename (new_db_table )
0 commit comments