Skip to content

Commit 6f75504

Browse files
committed
add more docs
1 parent dd6b87f commit 6f75504

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

docs/source/ref/models/fields.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ These indexes use 0-based indexing.
256256
class Book(models.Model):
257257
author = EmbeddedModelField(Author)
258258

259-
See :doc:`/topics/embedded-models` for more details and examples.
259+
See :ref:`the embedded model topic guide <embeddedmodelfield-example>` for
260+
more details and examples.
260261

261262
.. admonition:: Migrations support is limited
262263

@@ -290,6 +291,9 @@ These indexes use 0-based indexing.
290291
If passed, the array will have a maximum size as specified, validated
291292
by forms and model validation, but not enforced by the database.
292293

294+
See :ref:`the embedded model topic guide <embeddedmodelarrayfield-example>`
295+
for more details and examples.
296+
293297
.. admonition:: Migrations support is limited
294298

295299
As described above for :class:`EmbeddedModelField`,

docs/source/topics/embedded-models.rst

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ Use :class:`~django_mongodb_backend.fields.EmbeddedModelField` and
66
your data using `embedded documents
77
<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data>`_.
88

9+
.. _embeddedmodelfield-example:
10+
11+
``EmbeddedModelField``
12+
----------------------
13+
914
The basics
10-
----------
15+
~~~~~~~~~~
1116

1217
Let's consider this example::
1318

@@ -47,11 +52,61 @@ Represented in BSON, Bob's structure looks like this:
4752
...
4853
}
4954
55+
Unless explicitly set, embedded models don't have a primary key value.
56+
5057
Querying ``EmbeddedModelField``
51-
-------------------------------
58+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5259

5360
You can query into an embedded model using the same double underscore syntax
5461
as relational fields. For example, to retrieve all customers who have an
5562
address with the city "New York"::
5663

5764
>>> Customer.objects.filter(address__city="New York")
65+
66+
.. _embeddedmodelarrayfield-example:
67+
68+
``EmbeddedModelArrayField``
69+
---------------------------
70+
71+
The basics
72+
~~~~~~~~~~
73+
74+
Let's consider this example::
75+
76+
from django_mongodb_backend.fields import EmbeddedModelArrayField
77+
from django_mongodb_backend.models import EmbeddedModel
78+
79+
class Customer(models.Model):
80+
name = models.CharField(...)
81+
addresses = EmbeddedModelArrayField("Address")
82+
...
83+
84+
class Address(EmbeddedModel):
85+
...
86+
city = models.CharField(...)
87+
88+
89+
The API is similar to that of Django's relational fields::
90+
91+
>>> Customer.objects.create(name="Bob", addresses=[Address(city="New York", ...)], ...)
92+
>>> bob = Customer.objects.get(name="Bob")
93+
>>> bob.addresses
94+
[<Address: Address object>]
95+
>>> bob.address[0].city
96+
'New York'
97+
98+
Represented in BSON, Bob's structure looks like this:
99+
100+
.. code-block:: js
101+
102+
{
103+
"_id": ObjectId(...),
104+
"name": "Bob",
105+
"address": [{
106+
...
107+
"city": "New York"
108+
}],
109+
...
110+
}
111+
112+
Unless explicitly set, embedded models don't have a primary key value.

0 commit comments

Comments
 (0)