-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`` | ||
------------------------------- | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters