3
3
4
4
from django .core .checks import Error , Warning
5
5
from django .db import NotSupportedError
6
- from django .db .backends .utils import names_digest , split_identifier
7
6
from django .db .models import FloatField , Index , IntegerField
8
7
from django .db .models .lookups import BuiltinLookup
9
8
from django .db .models .sql .query import Query
14
13
from django_mongodb_backend .fields import ArrayField
15
14
16
15
from .query_utils import process_rhs
17
- from .utils import get_field
18
16
19
17
MONGO_INDEX_OPERATORS = {
20
18
"exact" : "$eq" ,
@@ -63,7 +61,7 @@ def get_pymongo_index_model(self, model, schema_editor, field=None, unique=False
63
61
filter_expression [column ].update ({"$type" : field .db_type (schema_editor .connection )})
64
62
else :
65
63
for field_name , _ in self .fields_orders :
66
- field_ = get_field (model , field_name )
64
+ field_ = model . _meta . get_field (field_name )
67
65
filter_expression [field_ .column ].update (
68
66
{"$type" : field_ .db_type (schema_editor .connection )}
69
67
)
@@ -76,7 +74,7 @@ def get_pymongo_index_model(self, model, schema_editor, field=None, unique=False
76
74
# order is "" if ASCENDING or "DESC" if DESCENDING (see
77
75
# django.db.models.indexes.Index.fields_orders).
78
76
(
79
- column_prefix + get_field (model , field_name ).column ,
77
+ column_prefix + model . _meta . get_field (field_name ).column ,
80
78
ASCENDING if order == "" else DESCENDING ,
81
79
)
82
80
for field_name , order in self .fields_orders
@@ -156,7 +154,7 @@ def get_pymongo_index_model(
156
154
for field_name , _ in self .fields_orders :
157
155
field = model ._meta .get_field (field_name )
158
156
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
160
158
fields [field_path ] = {"type" : type_ }
161
159
return SearchIndexModel (
162
160
definition = {"mappings" : {"dynamic" : False , "fields" : fields }}, name = self .name
@@ -266,7 +264,7 @@ def get_pymongo_index_model(
266
264
fields = []
267
265
for field_name , _ in self .fields_orders :
268
266
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
270
268
mappings = {"path" : field_path }
271
269
if isinstance (field_ , ArrayField ):
272
270
mappings .update (
@@ -282,38 +280,8 @@ def get_pymongo_index_model(
282
280
return SearchIndexModel (definition = {"fields" : fields }, name = self .name , type = "vectorSearch" )
283
281
284
282
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
-
314
283
def register_indexes ():
315
284
BuiltinLookup .as_mql_idx = builtin_lookup_idx
316
285
Index ._get_condition_mql = _get_condition_mql
317
286
Index .get_pymongo_index_model = get_pymongo_index_model
318
- Index .set_name_with_model = set_name_with_model
319
287
WhereNode .as_mql_idx = where_node_idx
0 commit comments