Skip to content

DOCSP-50489: EmbeddedModelArrayField support #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion source/includes/model-data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@ class Meta:

def __str__(self):
return self.title
# end-embedded-field
# 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
7 changes: 2 additions & 5 deletions source/limitations-upcoming.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- ✓

Expand Down
37 changes: 34 additions & 3 deletions source/model-data/models.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ The following table describes supported BSON field types and their
in this guide.

* - ``Object``
- ``EmbeddedModelField``
- | Stores embedded documents. To learn more about using this field
with {+django-odm+}, see the :ref:`django-models-embedded` section in this guide.
- ``EmbeddedModelField`` or ``EmbeddedModelArrayField``
- | 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``
Expand Down Expand Up @@ -452,6 +453,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``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In the Mongo parlance, isn't an "object" typically called a "document" and if so an EmbeddedModelArrayField could also be called a "document of documents" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think document is the better word, changed

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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: there might be confusion between document values and values that are documents
I.e.

  • document values of {hello: "world"} -> "world"
  • values that are documents - [{hello: "world"}, {goodbye: "sun"}]
Suggested change
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:
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``,
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 <django-models-define-ex>` 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
----------------------

Expand Down
Loading