From 7f891023e3be3e0e5e32ba579e259eff6d984751 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 5 Jun 2025 11:54:11 -0400 Subject: [PATCH 1/5] DOCSP-50489: EmbeddedModelArrayField support --- source/includes/model-data/models.py | 27 ++++++++++++++++++++++- source/model-data/models.txt | 32 +++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/source/includes/model-data/models.py b/source/includes/model-data/models.py index 1962fa9..a426fba 100644 --- a/source/includes/model-data/models.py +++ b/source/includes/model-data/models.py @@ -79,4 +79,29 @@ class Meta: def __str__(self): return self.title -# end-embedded-field \ No newline at end of file +# end-embedded-field + +# start-embedded-array-field +from django.db import models +from django_mongodb_backend.models import EmbeddedModel +from django_mongodb_backend.fields import EmbeddedModelArrayField + +class Actor(EmbeddedModel): + first_name = models.CharField(max_length=100) + last_name = models.CharField(max_length=100) + role = models.CharField(max_length=100) + +class Movie(models.Model): + title = models.CharField(max_length=200) + plot = models.TextField(blank=True) + runtime = models.IntegerField(default=0) + released = models.DateTimeField("release date", null=True, blank=True) + cast = EmbeddedModelArrayField(Actor, null=True, blank=True) + + class Meta: + db_table = "movies" + managed = False + + def __str__(self): + return self.title +# end-embedded-array-field \ No newline at end of file diff --git a/source/model-data/models.txt b/source/model-data/models.txt index 5c20703..58c10ee 100644 --- a/source/model-data/models.txt +++ b/source/model-data/models.txt @@ -168,7 +168,7 @@ The following table describes supported BSON field types and their in this guide. * - ``Object`` - - ``EmbeddedModelField`` + - ``EmbeddedModelField`` or ``EmbeddedModelArrayField`` - | Stores embedded documents. To learn more about using this field with {+django-odm+}, see the :ref:`django-models-embedded` section in this guide. @@ -452,6 +452,36 @@ and modifies the ``Movie`` model to include the ``EmbeddedModelField``: To learn how to query data stored in an ``EmbeddedModelField``, see :ref:`django-query-embedded` in the Specify a Query guide. +.. _django-models-embedded-array: + +Use an EmbeddedModelArrayField +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can use an ``EmbeddedModelArrayField`` to represent a MongoDB ``Object`` +that stores an array of document values. Each document in the array corresponds +to a {+django-odm+} ``EmbeddedModelField`` value. To create an ``EmbeddedModelArrayField``, +use the ``EmbeddedModelArrayField()`` class constructor and pass the following arguments: + +- ``embedded_model``: Specifies the model stored in each array item. + +- ``max_size``: *(Optional)* Specifies the maximum size of the array. + +Example +``````` + +This example adds an ``EmbeddedModelArrayField`` value to the model created in +the :ref:`Define a Model example ` in this +guide. This ``cast`` field stores an array of embedded ``Actor`` models. +The following code defines the ``Actor`` model and modifies the ``Movie`` model +to include the ``EmbeddedModelArrayField``: + +.. literalinclude:: /includes/model-data/models.py + :start-after: start-embedded-array-field + :end-before: end-embedded-array-field + :language: python + :copyable: + :emphasize-lines: 5, 15 + Additional Information ---------------------- From f228deff27ad8bdb0ee39d331f5dcb5393ada5bb Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 5 Jun 2025 11:56:53 -0400 Subject: [PATCH 2/5] edit limitations --- source/limitations-upcoming.txt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index 0141e81..2b3510a 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -108,11 +108,8 @@ in the following table. To view a full list of supported types, see the - Planned GA Support * - ``Array`` - - *Partially Supported*. You can use the ``ArrayField`` field type with the - following limitations: - - - ``ArrayField`` polymorphism is not supported. - - Nested ``EmbeddedModelField`` values within an ``ArrayField`` are not supported. + - *Partially Supported*. You can use the ``ArrayField`` field type, but + ``ArrayField`` polymorphism is not supported. - ✓ From 3f4ae23000715b3dfaae2cdf50b68d0a99637195 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 5 Jun 2025 12:02:31 -0400 Subject: [PATCH 3/5] edits --- source/model-data/models.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/model-data/models.txt b/source/model-data/models.txt index 58c10ee..ad38e9e 100644 --- a/source/model-data/models.txt +++ b/source/model-data/models.txt @@ -169,8 +169,9 @@ The following table describes supported BSON field types and their * - ``Object`` - ``EmbeddedModelField`` or ``EmbeddedModelArrayField`` - - | Stores embedded documents. To learn more about using this field - with {+django-odm+}, see the :ref:`django-models-embedded` section in this guide. + - | Stores one or multiple embedded documents. To learn more about using these fields + with {+django-odm+}, see the :ref:`django-models-embedded` and :ref:`django-models-embedded-array` + sections. * - ``ObjectId`` - ``ObjectIdField`` From 8fd7b28550c5052d04fbf2d21cb96562cdde0749 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 5 Jun 2025 13:45:44 -0400 Subject: [PATCH 4/5] RR feedback --- source/model-data/models.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/model-data/models.txt b/source/model-data/models.txt index ad38e9e..7bfa36c 100644 --- a/source/model-data/models.txt +++ b/source/model-data/models.txt @@ -459,7 +459,7 @@ Use an EmbeddedModelArrayField ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can use an ``EmbeddedModelArrayField`` to represent a MongoDB ``Object`` -that stores an array of document values. Each document in the array corresponds +that stores an array of documents. Each document in the array corresponds to a {+django-odm+} ``EmbeddedModelField`` value. To create an ``EmbeddedModelArrayField``, use the ``EmbeddedModelArrayField()`` class constructor and pass the following arguments: From 9374e2ecea2b2f290a4d1cc1949db79581a4dfa5 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 6 Jun 2025 15:56:19 -0400 Subject: [PATCH 5/5] AC feedback --- source/model-data/models.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/model-data/models.txt b/source/model-data/models.txt index 7bfa36c..87d2f88 100644 --- a/source/model-data/models.txt +++ b/source/model-data/models.txt @@ -458,9 +458,9 @@ and modifies the ``Movie`` model to include the ``EmbeddedModelField``: Use an EmbeddedModelArrayField ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -You can use an ``EmbeddedModelArrayField`` to represent a MongoDB ``Object`` -that stores an array of documents. Each document in the array corresponds -to a {+django-odm+} ``EmbeddedModelField`` value. To create an ``EmbeddedModelArrayField``, +You can use an ``EmbeddedModelArrayField`` to represent a MongoDB document +field that stores an array of documents in a one-to-many relationship. Each +document in the array corresponds to a {+django-odm+} ``EmbeddedModelField`` value. To create an ``EmbeddedModelArrayField``, use the ``EmbeddedModelArrayField()`` class constructor and pass the following arguments: - ``embedded_model``: Specifies the model stored in each array item.