Skip to content

Commit 78bea1c

Browse files
committed
wip docs
1 parent 4ed377e commit 78bea1c

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@ Model field reference
55

66
Some MongoDB-specific fields are available in ``django_mongodb.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.
20+
21+
The embedded model cannot have relational fields
22+
(:class:`~django.db.models.ForeignKey`,
23+
:class:`~django.db.models.OneToOneField` and
24+
:class:`~django.db.models.ManyToManyField`).
25+
26+
It is possible to nest embedded models. For example::
27+
28+
from django.db import models
29+
from django_mongodb.fields import EmbeddedModelField
30+
31+
32+
class ChessBoard(models.Model):
33+
board = EmbeddedModelField(
34+
ArrayField(
35+
models.CharField(max_length=10, blank=True),
36+
size=8,
37+
),
38+
size=8,
39+
)
40+
841
``ObjectIdField``
942
-----------------
1043

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)