44
55
66class DatabaseSchemaEditor (BaseDatabaseSchemaEditor ):
7+ def get_collection (self , name ):
8+ return self .connection .get_collection (name )
9+
710 @wrap_database_errors
811 def create_model (self , model ):
912 self .connection .database .create_collection (model ._meta .db_table )
@@ -17,7 +20,7 @@ def delete_model(self, model):
1720 for field in model ._meta .local_many_to_many :
1821 if field .remote_field .through ._meta .auto_created :
1922 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 ()
2124
2225 def add_field (self , model , field ):
2326 # Create implicit M2M tables.
@@ -26,7 +29,7 @@ def add_field(self, model, field):
2629 return
2730 # Set default value on existing documents.
2831 if column := field .column :
29- self .connection . database [ model ._meta .db_table ] .update_many (
32+ self .get_collection ( model ._meta .db_table ) .update_many (
3033 {}, [{"$set" : {column : self .effective_default (field )}}]
3134 )
3235
@@ -41,7 +44,7 @@ def _alter_field(
4144 new_db_params ,
4245 strict = False ,
4346 ):
44- collection = self .connection . database [ model ._meta .db_table ]
47+ collection = self .get_collection ( model ._meta .db_table )
4548 # Have they renamed the column?
4649 if old_field .column != new_field .column :
4750 collection .update_many ({}, {"$rename" : {old_field .column : new_field .column }})
@@ -59,7 +62,7 @@ def remove_field(self, model, field):
5962 return
6063 # Unset field on existing documents.
6164 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 : "" }})
6366
6467 def alter_index_together (self , model , old_index_together , new_index_together ):
6568 pass
@@ -85,4 +88,4 @@ def remove_constraint(self, model, constraint):
8588 def alter_db_table (self , model , old_db_table , new_db_table ):
8689 if old_db_table == new_db_table :
8790 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