@@ -77,14 +77,15 @@ models or use existing collections to store model data:
7777 python manage.py makemigrations <application name>
7878 python manage.py migrate
7979
80+ .. _django-models-define-ex:
81+
8082Example
8183```````
8284
83- This sample ``models.py`` file defines ``Movie`` and ``Award``
84- models. The ``Movie`` class includes the following information:
85+ This sample ``models.py`` file defines a ``Movie`` model
86+ class that includes the following information:
8587
86- - List of fields that represent movie data, including
87- an ``awards`` field that stores an embedded ``Award`` model.
88+ - List of fields that represent movie data.
8889
8990- ``Meta`` class that sets the ``db_table`` option
9091 to ``movies``. This instructs {+django-odm+} to use this model
@@ -98,15 +99,6 @@ models. The ``Movie`` class includes the following information:
9899- ``__str__()`` method that defines the model's string
99100 representation as its ``title`` field value.
100101
101- The ``Award`` model stores data for the ``Movie`` model's
102- ``awards`` field. Its class includes the following information:
103-
104- - List of fields that represent movie award data.
105-
106- - ``Meta`` class that sets the ``managed`` option to ``False``.
107- This instructs {+django-odm+} not to create a new collection
108- for the model.
109-
110102.. literalinclude:: /includes/model-data/models.py
111103 :start-after: start-models
112104 :end-before: end-models
@@ -116,7 +108,7 @@ The ``Award`` model stores data for the ``Movie`` model's
116108.. tip::
117109
118110 To learn more about the field types used in the model
119- class definitions , see the following :ref:`django-models-fields`
111+ class definition , see the following :ref:`django-models-fields`
120112 section of this guide.
121113
122114.. _django-models-fields:
@@ -235,18 +227,151 @@ Use a JSONField
235227
236228.. _django-models-mongodb-fields:
237229
238- MongoDB Fields
239- ~~~~~~~~~~~~~~
230+ MongoDB BSON Fields
231+ ~~~~~~~~~~~~~~~~~~~
232+
233+ MongoDB organizes and stores documents in a binary representation
234+ called ``BSON`` that allows for flexible data processing.
235+
236+ .. tip::
237+
238+ To learn more about how MongoDB stores BSON data, see
239+ :manual:`BSON Types </reference/bson-types>` in the {+mdb-server+}
240+ manual.
241+
242+ The following table describes supported BSON field types and their
243+ {+django-odm+} equivalents that you can use in your Django models:
244+
245+ .. list-table::
246+ :header-rows: 1
247+ :widths: 20 20 60
248+
249+ * - BSON Field Type
250+ - Django Field Type
251+ - BSON Description
252+
253+ * - ``Array``
254+ - ``ArrayField``
255+ - | Stores array values. To learn more about using this field
256+ with {+django-odm+}, see the :ref:`django-models-array` section
257+ in this guide.
258+
259+ * - ``Object``
260+ - ``EmbeddedModelField``
261+ - | Stores embedded documents. To learn more about using this field
262+ with {+django-odm+}, see the :ref:`django-models-embedded` section in this guide.
263+
264+ * - ``ObjectId``
265+ - ``AutoField``
266+ - | Stores unique 12-byte identifiers that MongoDB uses as primary keys.
267+
268+ * - ``Binary``
269+ - ``BinaryField``
270+ - | Stores binary data.
271+
272+ * - ``Boolean``
273+ - ``BooleanField``
274+ - | Stores ``true`` or ``false`` values.
275+
276+ * - ``Date``
277+ - ``DatetimeField``
278+ - | Stores dates and times in milliseconds since the Unix
279+ epoch, or January 1, 1970.
280+
281+ * - ``Decimal128``
282+ - ``DecimalField``
283+ - | Stores 28-bit decimal values.
284+
285+ * - ``Double``
286+ - ``FloatField``
287+ - | Stores floating-point values.
288+
289+ * - ``Int32``
290+ - ``IntegerField``
291+ - | Stores 32-bit signed integers.
292+
293+ * - ``Int64``
294+ - ``IntegerField`` or ``BigIntegerField``
295+ - | Stores 64-bit signed integers.
240296
241- The following table describes field types specific to
242- {+django-odm+} that you can use in your models:
297+ * - ``String``
298+ - ``CharField`` or ``TextField``
299+ - | Stores UTF-8 encoded string values.
300+
301+ .. _django-models-array:
243302
244303Use an ArrayField
245304`````````````````
246305
306+ You can use an ``ArrayField`` in your model to store a list of data.
307+ To create an ``ArrayField``, use the ``ArrayField()`` class constructor
308+ and pass the following arguments:
309+
310+ - ``base_field``: Specifies the underlying data type of each value
311+ stored in the array. You cannot specify ``EmbeddedModelField`` or
312+ ``FileField`` as the base field type.
313+
314+ - ``size``: Specifies the maximum size of the array. This field is
315+ optional.
316+
317+ - ``options``: Specifies Django field options. To view a list of
318+ available options, see `Field options <{+django-docs+}/ref/models/fields/#field-options>`__
319+ in the {+framework+} documentation.
320+
321+ The following example adds an ``ArrayField`` value to the model created in
322+ the :ref:`Define a Model example <django-models-define-ex>` in this
323+ guide. The new field, called ``genres``, stores a list of ``CharField`` values
324+ that represent movie genres and can store a maximum of ``5`` values:
325+
326+ .. literalinclude:: /includes/model-data/models.py
327+ :start-after: start-array-field
328+ :end-before: end-array-field
329+ :language: python
330+ :copyable:
331+ :emphasize-lines: 9-13
332+
333+ .. tip::
334+
335+ You can also store an array of array values in an ``ArrayField``.
336+ To view an example of a multi-dimensional array, see `ArrayField
337+ <{+django-docs+}/ref/contrib/postgres/fields/#arrayfield>`__ in the {+framework+}
338+ PostgreSQL documentation.
339+
340+ .. _django-models-embedded:
341+
247342Use an EmbeddedModelField
248343`````````````````````````
249344
345+ You can use an ``EmbeddedModelField`` to represent a MongoDB ``Object``,
346+ which stores a nested document value. This type allows one model to
347+ store a separate model in one of its fields. To create an ``EmbeddedModelField``, use
348+ the ``EmbeddedModelField()`` class constructor and pass the following arguments:
349+
350+ - ``embedded_model``: Specifies the model class to store.
351+
352+ - ``options``: Specifies Django field options. To view a list of
353+ available options, see `Field options <{+django-docs+}/ref/models/fields/#field-options>`__
354+ in the {+framework+} documentation.
355+
356+ .. important::
357+
358+ The ``makemigrations`` Django command does not detect changes to embedded
359+ models. If you make changes to the embedded model's class, the model
360+ stored in the ``EmbeddedModelField`` does not reflect the changes.
361+
362+ This example adds an ``EmbeddedModelField`` value to the model created in
363+ the :ref:`Define a Model example <django-models-define-ex>` in this
364+ guide. The new field, called ``awards``, stores an embedded ``Award`` model
365+ as its value. The following code defines the ``Award`` model
366+ and modifies the ``Movie`` model to include the ``EmbeddedModelField``:
367+
368+ .. literalinclude:: /includes/model-data/models.py
369+ :start-after: start-embedded-field
370+ :end-before: end-embedded-field
371+ :language: python
372+ :copyable:
373+ :emphasize-lines: 17
374+
250375Additional Information
251376----------------------
252377
0 commit comments