Skip to content

Commit b471b8a

Browse files
committed
Move mongo_data_types mapping
1 parent b08d2ae commit b471b8a

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
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
@@ -80,7 +80,6 @@ def create_mongodb_index(
8080
model,
8181
schema_editor,
8282
*,
83-
connection=None, # noqa: ARG001
8483
field=None,
8584
unique=False,
8685
column_prefix="",
@@ -126,17 +125,49 @@ def create_mongodb_index(
126125

127126
class SearchIndex(Index):
128127
suffix = "search"
128+
# Maps Django internal type to atlas search index type.
129+
mongo_data_types = {
130+
"AutoField": "number",
131+
"BigAutoField": "number",
132+
"BinaryField": "string",
133+
"BooleanField": "boolean",
134+
"CharField": "string",
135+
"DateField": "date",
136+
"DateTimeField": "date",
137+
"DecimalField": "number",
138+
"DurationField": "number",
139+
"FileField": "string",
140+
"FilePathField": "string",
141+
"FloatField": "double",
142+
"IntegerField": "number",
143+
"BigIntegerField": "number",
144+
"GenericIPAddressField": "string",
145+
"JSONField": "document",
146+
"OneToOneField": "number",
147+
"PositiveBigIntegerField": "number",
148+
"PositiveIntegerField": "number",
149+
"PositiveSmallIntegerField": "number",
150+
"SlugField": "string",
151+
"SmallAutoField": "number",
152+
"SmallIntegerField": "number",
153+
"TextField": "string",
154+
"TimeField": "date",
155+
"UUIDField": "uuid",
156+
"ObjectIdAutoField": "objectId",
157+
"ObjectIdField": "objectId",
158+
"EmbeddedModelField": "embeddedDocuments",
159+
}
129160

130161
def __init__(self, *expressions, **kwargs):
131162
super().__init__(*expressions, **kwargs)
132163

133164
def create_mongodb_index(
134-
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
165+
self, model, schema_editor, field=None, unique=False, column_prefix=""
135166
):
136167
fields = {}
137168
for field_name, _ in self.fields_orders:
138169
field_ = model._meta.get_field(field_name)
139-
type_ = connection.mongo_data_types[field_.get_internal_type()]
170+
type_ = self.mongo_data_types[field_.get_internal_type()]
140171
field_path = column_prefix + model._meta.get_field(field_name).column
141172
fields[field_path] = {"type": type_}
142173
return SearchIndexModel(
@@ -166,7 +197,7 @@ def _check_similarity_functions(self, similarities):
166197
)
167198

168199
def create_mongodb_index(
169-
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
200+
self, model, schema_editor, field=None, unique=False, column_prefix=""
170201
):
171202
similarities = (
172203
itertools.cycle([self.similarities])

django_mongodb_backend/schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ def add_index(
269269
field=field,
270270
unique=unique,
271271
column_prefix=column_prefix,
272-
connection=self.connection,
273272
)
274273
if idx:
275274
model = parent_model or model

0 commit comments

Comments
 (0)