Skip to content

Commit 2b0bded

Browse files
committed
wip docs
1 parent c132ff4 commit 2b0bded

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ repos:
4444
hooks:
4545
- id: rstcheck
4646
additional_dependencies: [sphinx]
47+
args: ["--ignore-directives=django-admin", "--ignore-roles=djadmin"]
4748

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

docs/source/_ext/djangodocs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def setup(app):
2+
app.add_crossref_type(
3+
directivename="fieldlookup",
4+
rolename="lookup",
5+
indextemplate="pair: %s; field lookup type",
6+
)
7+
app.add_crossref_type(
8+
directivename="setting",
9+
rolename="setting",
10+
indextemplate="pair: %s; setting",
11+
)
12+
app.add_object_type(
13+
directivename="django-admin",
14+
rolename="djadmin",
15+
indextemplate="pair: %s; django-admin command",
16+
)

docs/source/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
88
from __future__ import annotations
99

10+
import sys
1011
from importlib.metadata import version as _version
12+
from pathlib import Path
13+
14+
# If extensions (or modules to document with autodoc) are in another directory,
15+
# add these directories to sys.path here. If the directory is relative to the
16+
# documentation root, use os.path.abspath to make it absolute, like shown here.
17+
sys.path.append(str((Path(__file__).parent / "_ext").resolve()))
1118

1219
project = "django_mongodb_backend"
1320
copyright = "2024, The MongoDB Python Team"
@@ -22,6 +29,7 @@
2229
add_module_names = False
2330

2431
extensions = [
32+
"djangodocs",
2533
"sphinx.ext.intersphinx",
2634
]
2735

docs/source/embedded-models.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.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 very natural and is similar to that of Django's relation fields::
26+
27+
>>> Customer(name='Bob', address=Address(city='New York', ...), ...).save()
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+
49+
Querying ``EmbeddedModelField``
50+
-------------------------------
51+
52+
...

docs/source/fields.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@ Model field reference
55

66
Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
77

8+
``EmbeddedModelField``
9+
----------------------
10+
11+
.. class:: EmbeddedModelField(embedded_model, **kwargs)
12+
13+
Stores a model of type ``embedded_model``.
14+
15+
.. attribute:: embedded_model
16+
17+
This is a required argument.
18+
19+
Specifies the model class to embed. It can be either a concrete model
20+
class or a :ref:`lazy reference <lazy-relationships>` to a model class.
21+
22+
The embedded model cannot have relational fields
23+
(:class:`~django.db.models.ForeignKey`,
24+
:class:`~django.db.models.OneToOneField` and
25+
:class:`~django.db.models.ManyToManyField`).
26+
27+
It is possible to nest embedded models. For example::
28+
29+
from django.db import models
30+
from django_mongodb.fields import EmbeddedModelField
31+
32+
class Address(models.Model):
33+
...
34+
35+
class Author(models.Model):
36+
address = EmbeddedModelField(Address)
37+
38+
class Book(models.Model):
39+
author = EmbeddedModelField(Author)
40+
41+
.. admonition:: Migrations support is limited
42+
43+
:djadmin:`makemigrations` does not yet detect changes to embedded models.
44+
45+
After you create a model with an ``EmbeddedModelField`` or add an
46+
``EmbeddedModelField`` to an existing model, no further updates to the
47+
embedded model will be made. Using the models above as an example, if you
48+
created these models and then added an indexed field to ``Address``,
49+
the index created in the nested ``Book`` embed is not created.
50+
851
``ObjectIdField``
952
-----------------
1053

docs/source/index.rst

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

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

0 commit comments

Comments
 (0)