Skip to content

Commit

Permalink
wip docs
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 6, 2025
1 parent 4ed377e commit 78bea1c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/source/embedded-models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Embedded Models
===============

Use :class:`~django_mongdob.fields.EmbeddedModelField` to structure your data
using `embedded documents
<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data>`_.

The Basics
----------

Let's consider this example::

from django_mongodb.fields import EmbeddedModelField

class Customer(models.Model):
name = models.CharField(...)
address = EmbeddedModelField('Address')
...

class Address(models.Model):
...
city = models.CharField(...)


The API is very natural and is similar to that of Django's relation fields::

>>> Customer(name='Bob', address=Address(city='New York', ...), ...).save()
>>> bob = Customer.objects.get(...)
>>> bob.address
<Address: Address object>
>>> bob.address.city
'New York'

Represented in BSON, Bob's structure looks like this:

.. code-block:: js
{
"_id": ObjectId(...),
"name": "Bob",
"address": {
...
"city": "New York"
},
...
}
Querying ``EmbeddedModelField``
-------------------------------

...
33 changes: 33 additions & 0 deletions docs/source/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ Model field reference

Some MongoDB-specific fields are available in ``django_mongodb.fields``.

``EmbeddedModelField``
----------------------

.. class:: EmbeddedModelField(embedded_model, **kwargs)

Stores a model of type ``embedded_model``.

.. attribute:: embedded_model

This is a required argument.

Specifies the model class to embed.

The embedded model cannot have relational fields
(:class:`~django.db.models.ForeignKey`,
:class:`~django.db.models.OneToOneField` and
:class:`~django.db.models.ManyToManyField`).

It is possible to nest embedded models. For example::

from django.db import models
from django_mongodb.fields import EmbeddedModelField


class ChessBoard(models.Model):
board = EmbeddedModelField(
ArrayField(
models.CharField(max_length=10, blank=True),
size=8,
),
size=8,
)

``ObjectIdField``
-----------------

Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ django-mongodb 5.0.x documentation
fields
querysets
forms
embedded-models

Indices and tables
==================
Expand Down

0 comments on commit 78bea1c

Please sign in to comment.