Skip to content

Commit 8a0692b

Browse files
authored
DOCSP-50489: EmbeddedModelArrayField support (#37)
* DOCSP-50489: EmbeddedModelArrayField support * edit limitations * edits * RR feedback * AC feedback
1 parent 794ab48 commit 8a0692b

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

source/includes/model-data/models.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,29 @@ class Meta:
7979

8080
def __str__(self):
8181
return self.title
82-
# end-embedded-field
82+
# end-embedded-field
83+
84+
# start-embedded-array-field
85+
from django.db import models
86+
from django_mongodb_backend.models import EmbeddedModel
87+
from django_mongodb_backend.fields import EmbeddedModelArrayField
88+
89+
class Actor(EmbeddedModel):
90+
first_name = models.CharField(max_length=100)
91+
last_name = models.CharField(max_length=100)
92+
role = models.CharField(max_length=100)
93+
94+
class Movie(models.Model):
95+
title = models.CharField(max_length=200)
96+
plot = models.TextField(blank=True)
97+
runtime = models.IntegerField(default=0)
98+
released = models.DateTimeField("release date", null=True, blank=True)
99+
cast = EmbeddedModelArrayField(Actor, null=True, blank=True)
100+
101+
class Meta:
102+
db_table = "movies"
103+
managed = False
104+
105+
def __str__(self):
106+
return self.title
107+
# end-embedded-array-field

source/limitations-upcoming.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,8 @@ in the following table. To view a full list of supported types, see the
108108
- Planned GA Support
109109

110110
* - ``Array``
111-
- *Partially Supported*. You can use the ``ArrayField`` field type with the
112-
following limitations:
113-
114-
- ``ArrayField`` polymorphism is not supported.
115-
- Nested ``EmbeddedModelField`` values within an ``ArrayField`` are not supported.
111+
- *Partially Supported*. You can use the ``ArrayField`` field type, but
112+
``ArrayField`` polymorphism is not supported.
116113

117114
- ✓
118115

source/model-data/models.txt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ The following table describes supported BSON field types and their
168168
in this guide.
169169

170170
* - ``Object``
171-
- ``EmbeddedModelField``
172-
- | Stores embedded documents. To learn more about using this field
173-
with {+django-odm+}, see the :ref:`django-models-embedded` section in this guide.
171+
- ``EmbeddedModelField`` or ``EmbeddedModelArrayField``
172+
- | Stores one or multiple embedded documents. To learn more about using these fields
173+
with {+django-odm+}, see the :ref:`django-models-embedded` and :ref:`django-models-embedded-array`
174+
sections.
174175

175176
* - ``ObjectId``
176177
- ``ObjectIdField``
@@ -452,6 +453,36 @@ and modifies the ``Movie`` model to include the ``EmbeddedModelField``:
452453
To learn how to query data stored in an ``EmbeddedModelField``, see
453454
:ref:`django-query-embedded` in the Specify a Query guide.
454455

456+
.. _django-models-embedded-array:
457+
458+
Use an EmbeddedModelArrayField
459+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
460+
461+
You can use an ``EmbeddedModelArrayField`` to represent a MongoDB document
462+
field that stores an array of documents in a one-to-many relationship. Each
463+
document in the array corresponds to a {+django-odm+} ``EmbeddedModelField`` value. To create an ``EmbeddedModelArrayField``,
464+
use the ``EmbeddedModelArrayField()`` class constructor and pass the following arguments:
465+
466+
- ``embedded_model``: Specifies the model stored in each array item.
467+
468+
- ``max_size``: *(Optional)* Specifies the maximum size of the array.
469+
470+
Example
471+
```````
472+
473+
This example adds an ``EmbeddedModelArrayField`` value to the model created in
474+
the :ref:`Define a Model example <django-models-define-ex>` in this
475+
guide. This ``cast`` field stores an array of embedded ``Actor`` models.
476+
The following code defines the ``Actor`` model and modifies the ``Movie`` model
477+
to include the ``EmbeddedModelArrayField``:
478+
479+
.. literalinclude:: /includes/model-data/models.py
480+
:start-after: start-embedded-array-field
481+
:end-before: end-embedded-array-field
482+
:language: python
483+
:copyable:
484+
:emphasize-lines: 5, 15
485+
455486
Additional Information
456487
----------------------
457488

0 commit comments

Comments
 (0)