Skip to content

Commit 4f5d77e

Browse files
committed
INTPYTHON-624 Add PolymorphicEmbeddedModelField
1 parent 8c201d1 commit 4f5d77e

File tree

9 files changed

+670
-2
lines changed

9 files changed

+670
-2
lines changed

django_mongodb_backend/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ def _isnull_operator(a, b):
121121
"gte": lambda a, b: {"$gte": [a, b]},
122122
# MongoDB considers null less than zero. Exclude null values to match
123123
# SQL behavior.
124-
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, {"$ne": [a, None]}]},
125-
"lte": lambda a, b: {"$and": [{"$lte": [a, b]}, {"$ne": [a, None]}]},
124+
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, DatabaseWrapper._isnull_operator(a, False)]},
125+
"lte": lambda a, b: {
126+
"$and": [{"$lte": [a, b]}, DatabaseWrapper._isnull_operator(a, False)]
127+
},
126128
"in": lambda a, b: {"$in": [a, b]},
127129
"isnull": _isnull_operator,
128130
"range": lambda a, b: {

django_mongodb_backend/fields/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .embedded_model_array import EmbeddedModelArrayField
66
from .json import register_json_field
77
from .objectid import ObjectIdField
8+
from .polymorphic_embedded_model import PolymorphicEmbeddedModelField
89

910
__all__ = [
1011
"register_fields",
@@ -13,6 +14,7 @@
1314
"EmbeddedModelField",
1415
"ObjectIdAutoField",
1516
"ObjectIdField",
17+
"PolymorphicEmbeddedModelField",
1618
]
1719

1820

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import contextlib
2+
3+
from django.core import checks
4+
from django.core.exceptions import FieldDoesNotExist, ValidationError
5+
from django.db import connections, models
6+
from django.db.models.fields.related import lazy_related_operation
7+
8+
from .embedded_model import KeyTransformFactory
9+
10+
11+
class PolymorphicEmbeddedModelField(models.Field):
12+
"""Field that stores a model instance of varying type."""
13+
14+
stores_model_instance = True
15+
16+
def __init__(self, embedded_models, *args, **kwargs):
17+
"""
18+
`embedded_models` is a list of possible model classes to be stored.
19+
Like other relational fields, each model may also be passed as a
20+
string.
21+
"""
22+
self.embedded_models = embedded_models
23+
kwargs["editable"] = False
24+
super().__init__(*args, **kwargs)
25+
26+
def db_type(self, connection):
27+
return "embeddedDocuments"
28+
29+
def check(self, **kwargs):
30+
from ..models import EmbeddedModel
31+
32+
errors = super().check(**kwargs)
33+
embedded_fields = {}
34+
for model in self.embedded_models:
35+
if not issubclass(model, EmbeddedModel):
36+
return [
37+
checks.Error(
38+
"Embedded models must be a subclass of "
39+
"django_mongodb_backend.models.EmbeddedModel.",
40+
obj=self,
41+
hint="{model} doesn't subclass EmbeddedModel.",
42+
id="django_mongodb_backend.embedded_model.E002",
43+
)
44+
]
45+
for field in model._meta.fields:
46+
if field.remote_field:
47+
errors.append(
48+
checks.Error(
49+
"Embedded models cannot have relational fields "
50+
f"({model().__class__.__name__}.{field.name} "
51+
f"is a {field.__class__.__name__}).",
52+
obj=self,
53+
id="django_mongodb_backend.embedded_model.E001",
54+
)
55+
)
56+
field_name = field.name
57+
if existing_field := embedded_fields.get(field.name):
58+
connection = _get_mongodb_connection()
59+
if existing_field.db_type(connection) != field.db_type(connection):
60+
errors.append(
61+
checks.Warning(
62+
f"Embedded models {existing_field.model._meta.label} "
63+
f"and {field.model._meta.label} both have field "
64+
f"'{field_name}' of different type.",
65+
obj=self,
66+
id="django_mongodb_backend.embedded_model.E003",
67+
hint="It may be impossible to query both fields.",
68+
)
69+
)
70+
71+
else:
72+
embedded_fields[field_name] = field
73+
return errors
74+
75+
def deconstruct(self):
76+
name, path, args, kwargs = super().deconstruct()
77+
if path.startswith("django_mongodb_backend.fields.polymorphic_embedded_model"):
78+
path = path.replace(
79+
"django_mongodb_backend.fields.polymorphic_embedded_model",
80+
"django_mongodb_backend.fields",
81+
)
82+
kwargs["embedded_models"] = self.embedded_models
83+
del kwargs["editable"]
84+
return name, path, args, kwargs
85+
86+
def get_internal_type(self):
87+
return "PolymorphicEmbeddedModelField"
88+
89+
def _set_model(self, model):
90+
"""
91+
Resolve embedded model classes once the field knows the model it
92+
belongs to. If any of the items in __init__()'s embedded_models
93+
argument are strings, resolve each to the actual model class, similar
94+
to relational fields.
95+
"""
96+
self._model = model
97+
if model is not None:
98+
for embedded_model in self.embedded_models:
99+
if isinstance(embedded_model, str):
100+
101+
def _resolve_lookup(_, *resolved_models):
102+
self.embedded_models = resolved_models
103+
104+
lazy_related_operation(_resolve_lookup, model, *self.embedded_models)
105+
106+
model = property(lambda self: self._model, _set_model)
107+
108+
def from_db_value(self, value, expression, connection):
109+
return self.to_python(value)
110+
111+
def to_python(self, value):
112+
"""
113+
Pass embedded model fields' values through each field's to_python() and
114+
reinstantiate the embedded instance.
115+
"""
116+
if value is None:
117+
return None
118+
if not isinstance(value, dict):
119+
return value
120+
model_class = self._get_model_from_label(value.pop("_label"))
121+
instance = model_class(
122+
**{
123+
field.attname: field.to_python(value[field.attname])
124+
for field in model_class._meta.fields
125+
if field.attname in value
126+
}
127+
)
128+
instance._state.adding = False
129+
return instance
130+
131+
def get_db_prep_save(self, embedded_instance, connection):
132+
"""
133+
Apply pre_save() and get_db_prep_save() of embedded instance fields and
134+
create the {field: value} dict to be saved.
135+
"""
136+
if embedded_instance is None:
137+
return None
138+
if not isinstance(embedded_instance, self.embedded_models):
139+
raise TypeError(
140+
f"Expected instance of type {self.embedded_models!r}, not "
141+
f"{type(embedded_instance)!r}."
142+
)
143+
field_values = {}
144+
add = embedded_instance._state.adding
145+
for field in embedded_instance._meta.fields:
146+
value = field.get_db_prep_save(
147+
field.pre_save(embedded_instance, add), connection=connection
148+
)
149+
# Exclude unset primary keys (e.g. {'id': None}).
150+
if field.primary_key and value is None:
151+
continue
152+
field_values[field.attname] = value
153+
# Store the model's label to know the class to use for initializing
154+
# upon retrieval.
155+
field_values["_label"] = embedded_instance._meta.label
156+
# This instance will exist in the database soon.
157+
embedded_instance._state.adding = False
158+
return field_values
159+
160+
def get_transform(self, name):
161+
transform = super().get_transform(name)
162+
if transform:
163+
return transform
164+
for model in self.embedded_models:
165+
with contextlib.suppress(FieldDoesNotExist):
166+
field = model._meta.get_field(name)
167+
break
168+
else:
169+
raise FieldDoesNotExist(
170+
f"The models of field '{self.name}' have no field named '{name}'."
171+
)
172+
return KeyTransformFactory(name, field)
173+
174+
def validate(self, value, model_instance):
175+
super().validate(value, model_instance)
176+
if not isinstance(value, self.embedded_models):
177+
raise ValidationError(
178+
f"Expected instance of type {self.embedded_models!r}, not {type(value)!r}."
179+
)
180+
for field in value._meta.fields:
181+
attname = field.attname
182+
field.validate(getattr(value, attname), model_instance)
183+
184+
def formfield(self, **kwargs):
185+
raise NotImplementedError("PolymorphicEmbeddedModelField does not support forms.")
186+
187+
def _get_model_from_label(self, label):
188+
return next(model for model in self.embedded_models if model._meta.label == label)
189+
190+
191+
def _get_mongodb_connection():
192+
for alias in connections:
193+
if connections[alias].vendor == "mongodb":
194+
return connections[alias]
195+
return None

django_mongodb_backend/operations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def get_db_converters(self, expression):
122122
)
123123
elif internal_type == "JSONField":
124124
converters.append(self.convert_jsonfield_value)
125+
elif internal_type == "PolymorphicEmbeddedModelField":
126+
converters.append(self.convert_polymorphicembeddedmodelfield_value)
125127
elif internal_type == "TimeField":
126128
# Trunc(... output_field="TimeField") values must remain datetime
127129
# until Trunc.convert_value() so they can be converted from UTC
@@ -182,6 +184,19 @@ def convert_jsonfield_value(self, value, expression, connection):
182184
"""
183185
return json.dumps(value)
184186

187+
def convert_polymorphicembeddedmodelfield_value(self, value, expression, connection):
188+
if value is not None:
189+
model_class = expression.output_field._get_model_from_label(value["_label"])
190+
# Apply database converters to each field of the embedded model.
191+
for field in model_class._meta.fields:
192+
field_expr = Expression(output_field=field)
193+
converters = connection.ops.get_db_converters(
194+
field_expr
195+
) + field_expr.get_db_converters(connection)
196+
for converter in converters:
197+
value[field.attname] = converter(value[field.attname], field_expr, connection)
198+
return value
199+
185200
def convert_timefield_value(self, value, expression, connection):
186201
if value is not None:
187202
value = value.time()

docs/source/ref/models/fields.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,33 @@ These indexes use 0-based indexing.
313313
.. class:: ObjectIdField
314314

315315
Stores an :class:`~bson.objectid.ObjectId`.
316+
317+
``PolymorphicEmbeddedModelField``
318+
---------------------------------
319+
320+
.. class:: PolymorphicEmbeddedModelField(embedded_models, **kwargs)
321+
322+
.. versionadded:: 5.2.0b2
323+
324+
Stores a model of one of the types in ``embedded_models``.
325+
326+
.. attribute:: embedded_models
327+
328+
This is a required argument that specifies a list of model classes
329+
that may be embedded.
330+
331+
Each model class reference works just like
332+
:attr:`.EmbeddedModelField.embedded_model`.
333+
334+
See :ref:`the embedded model topic guide
335+
<polymorphic-embedded-model-field-example>` for more details and examples.
336+
337+
.. admonition:: Migrations support is limited
338+
339+
:djadmin:`makemigrations` does not yet detect changes to embedded models,
340+
nor does it create indexes or constraints for embedded models referenced
341+
by ``PolymorphicEmbeddedModelField``.
342+
343+
.. admonition:: Forms are not supported
344+
345+
``PolymorphicEmbeddedModelField``\s don't appear in model forms.

docs/source/releases/5.2.x.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ New features
1414
- Added the ``options`` parameter to
1515
:func:`~django_mongodb_backend.utils.parse_uri`.
1616
- Added support for :ref:`database transactions <transactions>`.
17+
- Added :class:`~.fields.PolymorphicEmbeddedModelField` for storing a model
18+
instance that may be of more than one model class.
1719

1820
5.2.0 beta 1
1921
============

0 commit comments

Comments
 (0)