Skip to content

Commit 5525d5d

Browse files
committed
wip docs
1 parent 0f86805 commit 5525d5d

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
hooks:
4545
- id: rstcheck
4646
additional_dependencies: [sphinx]
47-
args: ["--ignore-directives=fieldlookup,setting", "--ignore-roles=lookup,setting"]
47+
args: ["--ignore-directives=django-adminmfieldlookup,setting", "--ignore-roles=djadmin,lookup,setting"]
4848

4949
# We use the Python version instead of the original version which seems to require Docker
5050
# https://github.com/koalaman/shellcheck-precommit

docs/source/_ext/djangodocs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
def setup(app):
2+
app.add_object_type(
3+
directivename="django-admin",
4+
rolename="djadmin",
5+
indextemplate="pair: %s; django-admin command",
6+
)
27
app.add_crossref_type(
38
directivename="fieldlookup",
49
rolename="lookup",

docs/source/embedded-models.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Embedded Models
2+
===============
3+
4+
Use :class:`~django_mongdob.fields.EmbeddedModelField` to structure your data
5+
using `embedded documents
6+
<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data>`_.
7+
8+
The Basics
9+
----------
10+
11+
Let's consider this example::
12+
13+
from django_mongodb_backend.fields import EmbeddedModelField
14+
15+
class Customer(models.Model):
16+
name = models.CharField(...)
17+
address = EmbeddedModelField("Address")
18+
...
19+
20+
class Address(models.Model):
21+
...
22+
city = models.CharField(...)
23+
24+
25+
The API is similar to that of Django's relational fields::
26+
27+
>>> Customer.objects.create(name="Bob", address=Address(city="New York", ...), ...)
28+
>>> bob = Customer.objects.get(...)
29+
>>> bob.address
30+
<Address: Address object>
31+
>>> bob.address.city
32+
'New York'
33+
34+
Represented in BSON, Bob's structure looks like this:
35+
36+
.. code-block:: js
37+
38+
{
39+
"_id": ObjectId(...),
40+
"name": "Bob",
41+
"address": {
42+
...
43+
"city": "New York"
44+
},
45+
...
46+
}
47+
48+
Querying ``EmbeddedModelField``
49+
-------------------------------
50+
51+
You can query into an embedded model using the same double underscore syntax
52+
as relational fields. For example, to retrieve all customers who have an
53+
address with the city "New York"::
54+
55+
>>> Customer.objects.filter(address__city="New York")

docs/source/fields.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,51 @@ transform do not change. For example:
210210
211211
These indexes use 0-based indexing.
212212

213+
``EmbeddedModelField``
214+
----------------------
215+
216+
.. class:: EmbeddedModelField(embedded_model, **kwargs)
217+
218+
Stores a model of type ``embedded_model``.
219+
220+
.. attribute:: embedded_model
221+
222+
This is a required argument.
223+
224+
Specifies the model class to embed. It can be either a concrete model
225+
class or a :ref:`lazy reference <lazy-relationships>` to a model class.
226+
227+
The embedded model cannot have relational fields
228+
(:class:`~django.db.models.ForeignKey`,
229+
:class:`~django.db.models.OneToOneField` and
230+
:class:`~django.db.models.ManyToManyField`).
231+
232+
It is possible to nest embedded models. For example::
233+
234+
from django.db import models
235+
from django_mongodb_backend.fields import EmbeddedModelField
236+
237+
class Address(models.Model):
238+
...
239+
240+
class Author(models.Model):
241+
address = EmbeddedModelField(Address)
242+
243+
class Book(models.Model):
244+
author = EmbeddedModelField(Author)
245+
246+
See :doc:`embedded-models` for more details and examples.
247+
248+
.. admonition:: Migrations support is limited
249+
250+
:djadmin:`makemigrations` does not yet detect changes to embedded models.
251+
252+
After you create a model with an ``EmbeddedModelField`` or add an
253+
``EmbeddedModelField`` to an existing model, no further updates to the
254+
embedded model will be made. Using the models above as an example, if you
255+
created these models and then added an indexed field to ``Address``,
256+
the index created in the nested ``Book`` embed is not created.
257+
213258
``ObjectIdField``
214259
-----------------
215260

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ django-mongodb-backend 5.0.x documentation
88
fields
99
querysets
1010
forms
11+
embedded-models
1112

1213
Indices and tables
1314
==================

0 commit comments

Comments
 (0)