33
44from django .core .checks import Error , Warning
55from django .db import NotSupportedError
6- from django .db .backends .utils import names_digest , split_identifier
76from django .db .models import FloatField , Index , IntegerField
87from django .db .models .lookups import BuiltinLookup
98from django .db .models .sql .query import Query
1413from django_mongodb_backend .fields import ArrayField
1514
1615from .query_utils import process_rhs
17- from .utils import get_field
1816
1917MONGO_INDEX_OPERATORS = {
2018 "exact" : "$eq" ,
@@ -63,7 +61,7 @@ def get_pymongo_index_model(self, model, schema_editor, field=None, unique=False
6361 filter_expression [column ].update ({"$type" : field .db_type (schema_editor .connection )})
6462 else :
6563 for field_name , _ in self .fields_orders :
66- field_ = get_field (model , field_name )
64+ field_ = model . _meta . get_field (field_name )
6765 filter_expression [field_ .column ].update (
6866 {"$type" : field_ .db_type (schema_editor .connection )}
6967 )
@@ -76,7 +74,7 @@ def get_pymongo_index_model(self, model, schema_editor, field=None, unique=False
7674 # order is "" if ASCENDING or "DESC" if DESCENDING (see
7775 # django.db.models.indexes.Index.fields_orders).
7876 (
79- column_prefix + get_field (model , field_name ).column ,
77+ column_prefix + model . _meta . get_field (field_name ).column ,
8078 ASCENDING if order == "" else DESCENDING ,
8179 )
8280 for field_name , order in self .fields_orders
@@ -156,7 +154,7 @@ def get_pymongo_index_model(
156154 for field_name , _ in self .fields_orders :
157155 field = model ._meta .get_field (field_name )
158156 type_ = self .search_index_data_types (field .db_type (schema_editor .connection ))
159- field_path = column_prefix + get_field (model , field_name ).column
157+ field_path = column_prefix + model . _meta . get_field (field_name ).column
160158 fields [field_path ] = {"type" : type_ }
161159 return SearchIndexModel (
162160 definition = {"mappings" : {"dynamic" : False , "fields" : fields }}, name = self .name
@@ -266,7 +264,7 @@ def get_pymongo_index_model(
266264 fields = []
267265 for field_name , _ in self .fields_orders :
268266 field_ = model ._meta .get_field (field_name )
269- field_path = column_prefix + get_field (model , field_name ).column
267+ field_path = column_prefix + model . _meta . get_field (field_name ).column
270268 mappings = {"path" : field_path }
271269 if isinstance (field_ , ArrayField ):
272270 mappings .update (
@@ -282,38 +280,8 @@ def get_pymongo_index_model(
282280 return SearchIndexModel (definition = {"fields" : fields }, name = self .name , type = "vectorSearch" )
283281
284282
285- def set_name_with_model (self , model ):
286- """
287- Generate a unique name for the index.
288-
289- The name is divided into 3 parts - table name (12 chars), field name
290- (8 chars) and unique hash + suffix (10 chars). Each part is made to
291- fit its size by truncating the excess length.
292- """
293- _ , table_name = split_identifier (model ._meta .db_table )
294- column_names = [get_field (model , field_name ).column for field_name , order in self .fields_orders ]
295- column_names_with_order = [
296- (f"-{ column_name } " if order else column_name )
297- for column_name , (field_name , order ) in zip (column_names , self .fields_orders , strict = False )
298- ]
299- # The length of the parts of the name is based on the default max
300- # length of 30 characters.
301- hash_data = [table_name , * column_names_with_order , self .suffix ]
302- self .name = (
303- f"{ table_name [:11 ]} _{ column_names [0 ][:7 ]} _"
304- f"{ names_digest (* hash_data , length = 6 )} _{ self .suffix } "
305- )
306- if len (self .name ) > self .max_name_length :
307- raise ValueError (
308- "Index too long for multiple database support. Is self.suffix longer than 3 characters?"
309- )
310- if self .name [0 ] == "_" or self .name [0 ].isdigit ():
311- self .name = f"D{ self .name [1 :]} "
312-
313-
314283def register_indexes ():
315284 BuiltinLookup .as_mql_idx = builtin_lookup_idx
316285 Index ._get_condition_mql = _get_condition_mql
317286 Index .get_pymongo_index_model = get_pymongo_index_model
318- Index .set_name_with_model = set_name_with_model
319287 WhereNode .as_mql_idx = where_node_idx
0 commit comments