Skip to content

Commit c17e934

Browse files
committed
Move mongo_data_types mapping
1 parent 4de2211 commit c17e934

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

django_mongodb_backend/base.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,39 +127,6 @@ def _isnull_operator(a, b):
127127
"iregex": lambda a, b: regex_match(a, b, insensitive=True),
128128
}
129129

130-
# Maps Django internal type to atlas search index type.
131-
mongo_data_types = {
132-
"AutoField": "number",
133-
"BigAutoField": "number",
134-
"BinaryField": "string",
135-
"BooleanField": "boolean",
136-
"CharField": "string",
137-
"DateField": "date",
138-
"DateTimeField": "date",
139-
"DecimalField": "number",
140-
"DurationField": "number",
141-
"FileField": "string",
142-
"FilePathField": "string",
143-
"FloatField": "double",
144-
"IntegerField": "number",
145-
"BigIntegerField": "number",
146-
"GenericIPAddressField": "string",
147-
"JSONField": "document",
148-
"OneToOneField": "number",
149-
"PositiveBigIntegerField": "number",
150-
"PositiveIntegerField": "number",
151-
"PositiveSmallIntegerField": "number",
152-
"SlugField": "string",
153-
"SmallAutoField": "number",
154-
"SmallIntegerField": "number",
155-
"TextField": "string",
156-
"TimeField": "date",
157-
"UUIDField": "uuid",
158-
"ObjectIdAutoField": "objectId",
159-
"ObjectIdField": "objectId",
160-
"EmbeddedModelField": "embeddedDocuments",
161-
}
162-
163130
display_name = "MongoDB"
164131
vendor = "mongodb"
165132
Database = Database

django_mongodb_backend/indexes.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ def create_mongodb_index(
120120
model,
121121
schema_editor,
122122
*,
123-
connection=None, # noqa: ARG001
124123
field=None,
125124
unique=False,
126125
column_prefix="",
@@ -165,17 +164,49 @@ def create_mongodb_index(
165164

166165
class SearchIndex(Index):
167166
suffix = "search"
167+
# Maps Django internal type to atlas search index type.
168+
mongo_data_types = {
169+
"AutoField": "number",
170+
"BigAutoField": "number",
171+
"BinaryField": "string",
172+
"BooleanField": "boolean",
173+
"CharField": "string",
174+
"DateField": "date",
175+
"DateTimeField": "date",
176+
"DecimalField": "number",
177+
"DurationField": "number",
178+
"FileField": "string",
179+
"FilePathField": "string",
180+
"FloatField": "double",
181+
"IntegerField": "number",
182+
"BigIntegerField": "number",
183+
"GenericIPAddressField": "string",
184+
"JSONField": "document",
185+
"OneToOneField": "number",
186+
"PositiveBigIntegerField": "number",
187+
"PositiveIntegerField": "number",
188+
"PositiveSmallIntegerField": "number",
189+
"SlugField": "string",
190+
"SmallAutoField": "number",
191+
"SmallIntegerField": "number",
192+
"TextField": "string",
193+
"TimeField": "date",
194+
"UUIDField": "uuid",
195+
"ObjectIdAutoField": "objectId",
196+
"ObjectIdField": "objectId",
197+
"EmbeddedModelField": "embeddedDocuments",
198+
}
168199

169200
def __init__(self, *expressions, **kwargs):
170201
super().__init__(*expressions, **kwargs)
171202

172203
def create_mongodb_index(
173-
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
204+
self, model, schema_editor, field=None, unique=False, column_prefix=""
174205
):
175206
fields = {}
176207
for field_name, _ in self.fields_orders:
177208
field_ = model._meta.get_field(field_name)
178-
type_ = connection.mongo_data_types[field_.get_internal_type()]
209+
type_ = self.mongo_data_types[field_.get_internal_type()]
179210
field_path = column_prefix + model._meta.get_field(field_name).column
180211
fields[field_path] = {"type": type_}
181212
return SearchIndexModel(
@@ -205,7 +236,7 @@ def _check_similarity_functions(self, similarities):
205236
)
206237

207238
def create_mongodb_index(
208-
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
239+
self, model, schema_editor, field=None, unique=False, column_prefix=""
209240
):
210241
similarities = (
211242
itertools.cycle([self.similarities])

0 commit comments

Comments
 (0)