Skip to content

Commit 7296117

Browse files
committed
Patch option to support top label indexes.
1 parent 89bc095 commit 7296117

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

django_mongodb_backend/lookups.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import django.db.models.base as base
12
from django.db import NotSupportedError
3+
from django.db.models.constants import LOOKUP_SEP
24
from django.db.models.fields.related_lookups import In, RelatedIn
35
from django.db.models.lookups import (
46
BuiltinLookup,
@@ -8,6 +10,7 @@
810
UUIDTextMixin,
911
)
1012

13+
from .fields import EmbeddedModelField
1114
from .query_utils import process_lhs, process_rhs
1215

1316

@@ -121,6 +124,26 @@ def uuid_text_mixin(self, compiler, connection): # noqa: ARG001
121124
raise NotSupportedError("Pattern lookups on UUIDField are not supported.")
122125

123126

127+
class Options(base.Options):
128+
def get_field(self, field_name):
129+
if LOOKUP_SEP in field_name:
130+
previous = self
131+
keys = field_name.split(LOOKUP_SEP)
132+
path = []
133+
for field in keys:
134+
field = base.Options.get_field(previous, field)
135+
if isinstance(field, EmbeddedModelField):
136+
previous = field.embedded_model._meta
137+
else:
138+
previous = field
139+
path.append(field.column)
140+
column = ".".join(path)
141+
embedded_column = field.clone()
142+
embedded_column.column = column
143+
return embedded_column
144+
return super().get_field(field_name)
145+
146+
124147
def register_lookups():
125148
BuiltinLookup.as_mql = builtin_lookup
126149
FieldGetDbPrepValueIterableMixin.resolve_expression_parameter = (
@@ -131,3 +154,4 @@ def register_lookups():
131154
IsNull.as_mql = is_null
132155
PatternLookup.prep_lookup_value_mongo = pattern_lookup_prep_lookup_value
133156
UUIDTextMixin.as_mql = uuid_text_mixin
157+
base.Options = Options

django_mongodb_backend/schema.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
22
from django.db.models import Index, UniqueConstraint
3+
from django.db.models.constants import LOOKUP_SEP
34
from pymongo.operations import SearchIndexModel
45

56
from django_mongodb_backend.indexes import SearchIndex
@@ -184,6 +185,20 @@ def remove_field(self, model, field):
184185
self._remove_model_indexes(
185186
field.embedded_model, parent_model=model, column_prefix=new_path
186187
)
188+
# Remove the top level indexes.
189+
# TODO: Find a workaround
190+
for index in model._meta.indexes:
191+
if any(
192+
field_name.startswith(f"{field.column}{LOOKUP_SEP}")
193+
for field_name in index.fields
194+
):
195+
self.remove_index(model, index)
196+
for constraint in model._meta.constraints:
197+
if any(
198+
field_name.startswith(f"{field.column}{LOOKUP_SEP}")
199+
for field_name in constraint.fields
200+
):
201+
self.get_collection(model._meta.db_table).drop_index(constraint.name)
187202

188203
def _remove_model_indexes(self, model, column_prefix="", parent_model=None):
189204
"""

0 commit comments

Comments
 (0)